1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2019 Intel Corporation
11 * Generic, commonly-used macro and inline function definitions
25 #include <rte_config.h>
27 /* OS specific include */
31 #define typeof __typeof__
38 /** C extension macro for environments lacking C11 features. */
39 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
40 #define RTE_STD_C11 __extension__
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.
51 #define RTE_CC_IS_GNU 0
54 #elif defined __INTEL_COMPILER
56 #elif defined __GNUC__
59 #define RTE_CC_IS_GNU 1
62 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
66 #ifdef RTE_ARCH_STRICT_ALIGN
67 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
68 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
69 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
71 typedef uint64_t unaligned_uint64_t;
72 typedef uint32_t unaligned_uint32_t;
73 typedef uint16_t unaligned_uint16_t;
79 #define __rte_aligned(a) __attribute__((__aligned__(a)))
82 * Force a structure to be packed
84 #define __rte_packed __attribute__((__packed__))
86 /******* Macro to mark functions and fields scheduled for removal *****/
87 #define __rte_deprecated __attribute__((__deprecated__))
90 * Mark a function or variable to a weak reference.
92 #define __rte_weak __attribute__((__weak__))
94 /*********** Macros to eliminate unused variable warnings ********/
97 * short definition to mark a function parameter unused
99 #define __rte_unused __attribute__((__unused__))
102 * definition to mark a variable or function parameter as used so
103 * as to avoid a compiler warning
105 #define RTE_SET_USED(x) (void)(x)
107 #define RTE_PRIORITY_LOG 101
108 #define RTE_PRIORITY_BUS 110
109 #define RTE_PRIORITY_CLASS 120
110 #define RTE_PRIORITY_LAST 65535
112 #define RTE_PRIO(prio) \
113 RTE_PRIORITY_ ## prio
116 * Run function before main() with high priority.
119 * Constructor function.
121 * Priority number must be above 100.
122 * Lowest number is the first to run.
124 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
125 #define RTE_INIT_PRIO(func, prio) \
126 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
130 * Run function before main() with low priority.
132 * The constructor will be run after prioritized constructors.
135 * Constructor function.
137 #define RTE_INIT(func) \
138 RTE_INIT_PRIO(func, LAST)
141 * Run after main() with low priority.
144 * Destructor function name.
146 * Priority number must be above 100.
147 * Lowest number is the last to run.
149 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
150 #define RTE_FINI_PRIO(func, prio) \
151 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
155 * Run after main() with high priority.
157 * The destructor will be run *before* prioritized destructors.
160 * Destructor function name.
162 #define RTE_FINI(func) \
163 RTE_FINI_PRIO(func, LAST)
166 * Force a function to be inlined
168 #define __rte_always_inline inline __attribute__((always_inline))
171 * Force a function to be noinlined
173 #define __rte_noinline __attribute__((noinline))
175 /*********** Macros for pointer arithmetic ********/
178 * add a byte-value offset to a pointer
180 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
183 * subtract a byte-value offset from a pointer
185 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
188 * get the difference between two pointer values, i.e. how far apart
189 * in bytes are the locations they point two. It is assumed that
190 * ptr1 is greater than ptr2.
192 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
195 * Workaround to cast a const field of a structure to non-const type.
197 #define RTE_CAST_FIELD(var, field, type) \
198 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
200 /*********** Macros/static functions for doing alignment ********/
204 * Macro to align a pointer to a given power-of-two. The resultant
205 * pointer will be a pointer of the same type as the first parameter, and
206 * point to an address no higher than the first parameter. Second parameter
207 * must be a power-of-two value.
209 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
210 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
213 * Macro to align a value to a given power-of-two. The resultant value
214 * will be of the same type as the first parameter, and will be no
215 * bigger than the first parameter. Second parameter must be a
216 * power-of-two value.
218 #define RTE_ALIGN_FLOOR(val, align) \
219 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
222 * Macro to align a pointer to a given power-of-two. The resultant
223 * pointer will be a pointer of the same type as the first parameter, and
224 * point to an address no lower than the first parameter. Second parameter
225 * must be a power-of-two value.
227 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
228 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
231 * Macro to align a value to a given power-of-two. The resultant value
232 * will be of the same type as the first parameter, and will be no lower
233 * than the first parameter. Second parameter must be a power-of-two
236 #define RTE_ALIGN_CEIL(val, align) \
237 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
240 * Macro to align a pointer to a given power-of-two. The resultant
241 * pointer will be a pointer of the same type as the first parameter, and
242 * point to an address no lower than the first parameter. Second parameter
243 * must be a power-of-two value.
244 * This function is the same as RTE_PTR_ALIGN_CEIL
246 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
249 * Macro to align a value to a given power-of-two. The resultant
250 * value will be of the same type as the first parameter, and
251 * will be no lower than the first parameter. Second parameter
252 * must be a power-of-two value.
253 * This function is the same as RTE_ALIGN_CEIL
255 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
258 * Macro to align a value to the multiple of given value. The resultant
259 * value will be of the same type as the first parameter and will be no lower
260 * than the first parameter.
262 #define RTE_ALIGN_MUL_CEIL(v, mul) \
263 (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
266 * Macro to align a value to the multiple of given value. The resultant
267 * value will be of the same type as the first parameter and will be no higher
268 * than the first parameter.
270 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
271 ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
274 * Macro to align value to the nearest multiple of the given value.
275 * The resultant value might be greater than or less than the first parameter
276 * whichever difference is the lowest.
278 #define RTE_ALIGN_MUL_NEAR(v, mul) \
280 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
281 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
282 (ceil - v) > (v - floor) ? floor : ceil; \
286 * Checks if a pointer is aligned to a given power-of-two value
289 * The pointer whose alignment is to be checked
291 * The power-of-two value to which the ptr should be aligned
294 * True(1) where the pointer is correctly aligned, false(0) otherwise
297 rte_is_aligned(void *ptr, unsigned align)
299 return RTE_PTR_ALIGN(ptr, align) == ptr;
302 /*********** Macros for compile type checks ********/
305 * Triggers an error at compilation time if the condition is true.
307 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
309 /*********** Cache line related macros ********/
311 /** Cache line mask. */
312 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
314 /** Return the first cache-aligned value greater or equal to size. */
315 #define RTE_CACHE_LINE_ROUNDUP(size) \
316 (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
317 RTE_CACHE_LINE_SIZE))
319 /** Cache line size in terms of log2 */
320 #if RTE_CACHE_LINE_SIZE == 64
321 #define RTE_CACHE_LINE_SIZE_LOG2 6
322 #elif RTE_CACHE_LINE_SIZE == 128
323 #define RTE_CACHE_LINE_SIZE_LOG2 7
325 #error "Unsupported cache line size"
328 /** Minimum Cache line size. */
329 #define RTE_CACHE_LINE_MIN_SIZE 64
331 /** Force alignment to cache line. */
332 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
334 /** Force minimum cache line alignment. */
335 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
337 /*********** PA/IOVA type definitions ********/
339 /** Physical address */
340 typedef uint64_t phys_addr_t;
341 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
344 * IO virtual address type.
345 * When the physical addressing mode (IOVA as PA) is in use,
346 * the translation from an IO virtual address (IOVA) to a physical address
347 * is a direct mapping, i.e. the same value.
348 * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
350 typedef uint64_t rte_iova_t;
351 #define RTE_BAD_IOVA ((rte_iova_t)-1)
353 /*********** Structure alignment markers ********/
355 /** Generic marker for any place in a structure. */
356 __extension__ typedef void *RTE_MARKER[0];
357 /** Marker for 1B alignment in a structure. */
358 __extension__ typedef uint8_t RTE_MARKER8[0];
359 /** Marker for 2B alignment in a structure. */
360 __extension__ typedef uint16_t RTE_MARKER16[0];
361 /** Marker for 4B alignment in a structure. */
362 __extension__ typedef uint32_t RTE_MARKER32[0];
363 /** Marker for 8B alignment in a structure. */
364 __extension__ typedef uint64_t RTE_MARKER64[0];
367 * Combines 32b inputs most significant set bits into the least
368 * significant bits to construct a value with the same MSBs as x
369 * but all 1's under it.
372 * The integer whose MSBs need to be combined with its LSBs
374 * The combined value.
376 static inline uint32_t
377 rte_combine32ms1b(register uint32_t x)
389 * Combines 64b inputs most significant set bits into the least
390 * significant bits to construct a value with the same MSBs as x
391 * but all 1's under it.
394 * The integer whose MSBs need to be combined with its LSBs
396 * The combined value.
398 static inline uint64_t
399 rte_combine64ms1b(register uint64_t v)
411 /*********** Macros to work with powers of 2 ********/
414 * Macro to return 1 if n is a power of 2, 0 otherwise
416 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
419 * Returns true if n is a power of 2
422 * @return 1 if true, 0 otherwise
425 rte_is_power_of_2(uint32_t n)
427 return n && !(n & (n - 1));
431 * Aligns input parameter to the next power of 2
434 * The integer value to align
437 * Input parameter aligned to the next power of 2
439 static inline uint32_t
440 rte_align32pow2(uint32_t x)
443 x = rte_combine32ms1b(x);
449 * Aligns input parameter to the previous power of 2
452 * The integer value to align
455 * Input parameter aligned to the previous power of 2
457 static inline uint32_t
458 rte_align32prevpow2(uint32_t x)
460 x = rte_combine32ms1b(x);
466 * Aligns 64b input parameter to the next power of 2
469 * The 64b value to align
472 * Input parameter aligned to the next power of 2
474 static inline uint64_t
475 rte_align64pow2(uint64_t v)
478 v = rte_combine64ms1b(v);
484 * Aligns 64b input parameter to the previous power of 2
487 * The 64b value to align
490 * Input parameter aligned to the previous power of 2
492 static inline uint64_t
493 rte_align64prevpow2(uint64_t v)
495 v = rte_combine64ms1b(v);
500 /*********** Macros for calculating min and max **********/
503 * Macro to return the minimum of two numbers
505 #define RTE_MIN(a, b) \
507 typeof (a) _a = (a); \
508 typeof (b) _b = (b); \
513 * Macro to return the maximum of two numbers
515 #define RTE_MAX(a, b) \
517 typeof (a) _a = (a); \
518 typeof (b) _b = (b); \
522 /*********** Other general functions / macros ********/
525 * Searches the input parameter for the least significant set bit
526 * (starting from zero).
527 * If a least significant 1 bit is found, its bit index is returned.
528 * If the content of the input parameter is zero, then the content of the return
529 * value is undefined.
531 * input parameter, should not be zero.
533 * least significant set bit in the input parameter.
535 static inline uint32_t
536 rte_bsf32(uint32_t v)
538 return (uint32_t)__builtin_ctz(v);
542 * Searches the input parameter for the least significant set bit
543 * (starting from zero). Safe version (checks for input parameter being zero).
545 * @warning ``pos`` must be a valid pointer. It is not checked!
548 * The input parameter.
550 * If ``v`` was not 0, this value will contain position of least significant
551 * bit within the input parameter.
553 * Returns 0 if ``v`` was 0, otherwise returns 1.
556 rte_bsf32_safe(uint64_t v, uint32_t *pos)
566 * Return the rounded-up log2 of a integer.
568 * @note Contrary to the logarithm mathematical operation,
569 * rte_log2_u32(0) == 0 and not -inf.
572 * The input parameter.
574 * The rounded-up log2 of the input, or 0 if the input is 0.
576 static inline uint32_t
577 rte_log2_u32(uint32_t v)
581 v = rte_align32pow2(v);
587 * Return the last (most-significant) bit set.
589 * @note The last (most significant) bit is at position 32.
590 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
593 * The input parameter.
595 * The last (most-significant) bit set, or 0 if the input is 0.
598 rte_fls_u32(uint32_t x)
600 return (x == 0) ? 0 : 32 - __builtin_clz(x);
604 * Searches the input parameter for the least significant set bit
605 * (starting from zero).
606 * If a least significant 1 bit is found, its bit index is returned.
607 * If the content of the input parameter is zero, then the content of the return
608 * value is undefined.
610 * input parameter, should not be zero.
612 * least significant set bit in the input parameter.
615 rte_bsf64(uint64_t v)
617 return (uint32_t)__builtin_ctzll(v);
621 * Searches the input parameter for the least significant set bit
622 * (starting from zero). Safe version (checks for input parameter being zero).
624 * @warning ``pos`` must be a valid pointer. It is not checked!
627 * The input parameter.
629 * If ``v`` was not 0, this value will contain position of least significant
630 * bit within the input parameter.
632 * Returns 0 if ``v`` was 0, otherwise returns 1.
635 rte_bsf64_safe(uint64_t v, uint32_t *pos)
645 * Return the last (most-significant) bit set.
647 * @note The last (most significant) bit is at position 64.
648 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
649 * rte_fls_u64(0x8000000000000000) = 64
652 * The input parameter.
654 * The last (most-significant) bit set, or 0 if the input is 0.
657 rte_fls_u64(uint64_t x)
659 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
663 * Return the rounded-up log2 of a 64-bit integer.
665 * @note Contrary to the logarithm mathematical operation,
666 * rte_log2_u64(0) == 0 and not -inf.
669 * The input parameter.
671 * The rounded-up log2 of the input, or 0 if the input is 0.
673 static inline uint32_t
674 rte_log2_u64(uint64_t v)
678 v = rte_align64pow2(v);
679 /* we checked for v being 0 already, so no undefined behavior */
684 /** Return the offset of a field in a structure. */
685 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
689 * Return pointer to the wrapping struct instance.
699 * struct child *x = obtain(...);
700 * struct wrapper *w = container_of(x, struct wrapper, c);
703 #define container_of(ptr, type, member) __extension__ ({ \
704 const typeof(((type *)0)->member) *_ptr = (ptr); \
705 __attribute__((unused)) type *_target_ptr = \
707 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
712 * Get the size of a field in a structure.
715 * The type of the structure.
717 * The field in the structure.
719 * The size of the field in the structure, in bytes.
721 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
723 #define _RTE_STR(x) #x
724 /** Take a macro value and get a string version of it */
725 #define RTE_STR(x) _RTE_STR(x)
728 * ISO C helpers to modify format strings using variadic macros.
729 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
730 * An empty %s argument is appended to avoid a dangling comma.
732 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
733 #define RTE_FMT_HEAD(fmt, ...) fmt
734 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
736 /** Mask value of type "tp" for the first "ln" bit set. */
737 #define RTE_LEN2MASK(ln, tp) \
738 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
740 /** Number of elements in the array. */
741 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
744 * Converts a numeric string to the equivalent uint64_t value.
745 * As well as straight number conversion, also recognises the suffixes
746 * k, m and g for kilobytes, megabytes and gigabytes respectively.
748 * If a negative number is passed in i.e. a string with the first non-black
749 * character being "-", zero is returned. Zero is also returned in the case of
750 * an error with the strtoull call in the function.
753 * String containing number to convert.
757 static inline uint64_t
758 rte_str_to_size(const char *str)
761 unsigned long long size;
763 while (isspace((int)*str))
769 size = strtoull(str, &endptr, 0);
774 endptr++; /* allow 1 space gap */
777 case 'G': case 'g': size *= 1024; /* fall-through */
778 case 'M': case 'm': size *= 1024; /* fall-through */
779 case 'K': case 'k': size *= 1024; /* fall-through */
787 * Function to terminate the application immediately, printing an error
788 * message and returning the exit_code back to the shell.
790 * This function never returns
793 * The exit code to be returned by the application
795 * The format string to be used for printing the message. This can include
796 * printf format characters which will be expanded using any further parameters
800 rte_exit(int exit_code, const char *format, ...)
801 __attribute__((noreturn))
802 __attribute__((format(printf, 2, 3)));