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__
40 /** C extension macro for environments lacking C11 features. */
41 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
42 #define RTE_STD_C11 __extension__
48 * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
49 * while a host application (like pmdinfogen) may have another compiler.
50 * RTE_CC_IS_GNU is true if the file is compiled with GCC,
51 * no matter it is a target or host application.
53 #define RTE_CC_IS_GNU 0
56 #elif defined __INTEL_COMPILER
58 #elif defined __GNUC__
61 #define RTE_CC_IS_GNU 1
64 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
71 #define __rte_aligned(a) __attribute__((__aligned__(a)))
73 #ifdef RTE_ARCH_STRICT_ALIGN
74 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
75 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
76 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
78 typedef uint64_t unaligned_uint64_t;
79 typedef uint32_t unaligned_uint32_t;
80 typedef uint16_t unaligned_uint16_t;
84 * Force a structure to be packed
86 #define __rte_packed __attribute__((__packed__))
88 /******* Macro to mark functions and fields scheduled for removal *****/
89 #define __rte_deprecated __attribute__((__deprecated__))
90 #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
93 * Macro to mark macros and defines scheduled for removal
95 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
96 #define RTE_PRAGMA(x) _Pragma(#x)
97 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
98 #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
100 #define RTE_DEPRECATED(x)
104 * Mark a function or variable to a weak reference.
106 #define __rte_weak __attribute__((__weak__))
109 * Force symbol to be generated even if it appears to be unused.
111 #define __rte_used __attribute__((used))
113 /*********** Macros to eliminate unused variable warnings ********/
116 * short definition to mark a function parameter unused
118 #define __rte_unused __attribute__((__unused__))
121 * Mark pointer as restricted with regard to pointer aliasing.
123 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
124 #define __rte_restrict __restrict
126 #define __rte_restrict restrict
130 * definition to mark a variable or function parameter as used so
131 * as to avoid a compiler warning
133 #define RTE_SET_USED(x) (void)(x)
136 * Check format string and its arguments at compile-time.
138 * GCC on Windows assumes MS-specific format string by default,
139 * even if the underlying stdio implementation is ANSI-compliant,
140 * so this must be overridden.
143 #define __rte_format_printf(format_index, first_arg) \
144 __attribute__((format(gnu_printf, format_index, first_arg)))
146 #define __rte_format_printf(format_index, first_arg) \
147 __attribute__((format(printf, format_index, first_arg)))
151 * Tells compiler that the function returns a value that points to
152 * memory, where the size is given by the one or two arguments.
153 * Used by compiler to validate object size.
155 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
156 #define __rte_alloc_size(...) \
157 __attribute__((alloc_size(__VA_ARGS__)))
159 #define __rte_alloc_size(...)
162 #define RTE_PRIORITY_LOG 101
163 #define RTE_PRIORITY_BUS 110
164 #define RTE_PRIORITY_CLASS 120
165 #define RTE_PRIORITY_LAST 65535
167 #define RTE_PRIO(prio) \
168 RTE_PRIORITY_ ## prio
171 * Run function before main() with high priority.
174 * Constructor function.
176 * Priority number must be above 100.
177 * Lowest number is the first to run.
179 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
180 #define RTE_INIT_PRIO(func, prio) \
181 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
185 * Run function before main() with low priority.
187 * The constructor will be run after prioritized constructors.
190 * Constructor function.
192 #define RTE_INIT(func) \
193 RTE_INIT_PRIO(func, LAST)
196 * Run after main() with low priority.
199 * Destructor function name.
201 * Priority number must be above 100.
202 * Lowest number is the last to run.
204 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
205 #define RTE_FINI_PRIO(func, prio) \
206 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
210 * Run after main() with high priority.
212 * The destructor will be run *before* prioritized destructors.
215 * Destructor function name.
217 #define RTE_FINI(func) \
218 RTE_FINI_PRIO(func, LAST)
221 * Hint never returning function
223 #define __rte_noreturn __attribute__((noreturn))
226 * Force a function to be inlined
228 #define __rte_always_inline inline __attribute__((always_inline))
231 * Force a function to be noinlined
233 #define __rte_noinline __attribute__((noinline))
236 * Hint function in the hot path
238 #define __rte_hot __attribute__((hot))
241 * Hint function in the cold path
243 #define __rte_cold __attribute__((cold))
245 /*********** Macros for pointer arithmetic ********/
248 * add a byte-value offset to a pointer
250 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
253 * subtract a byte-value offset from a pointer
255 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
258 * get the difference between two pointer values, i.e. how far apart
259 * in bytes are the locations they point two. It is assumed that
260 * ptr1 is greater than ptr2.
262 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
265 * Workaround to cast a const field of a structure to non-const type.
267 #define RTE_CAST_FIELD(var, field, type) \
268 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
270 /*********** Macros/static functions for doing alignment ********/
274 * Macro to align a pointer to a given power-of-two. The resultant
275 * pointer will be a pointer of the same type as the first parameter, and
276 * point to an address no higher than the first parameter. Second parameter
277 * must be a power-of-two value.
279 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
280 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
283 * Macro to align a value to a given power-of-two. The resultant value
284 * will be of the same type as the first parameter, and will be no
285 * bigger than the first parameter. Second parameter must be a
286 * power-of-two value.
288 #define RTE_ALIGN_FLOOR(val, align) \
289 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
292 * Macro to align a pointer to a given power-of-two. The resultant
293 * pointer will be a pointer of the same type as the first parameter, and
294 * point to an address no lower than the first parameter. Second parameter
295 * must be a power-of-two value.
297 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
298 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
301 * Macro to align a value to a given power-of-two. The resultant value
302 * will be of the same type as the first parameter, and will be no lower
303 * than the first parameter. Second parameter must be a power-of-two
306 #define RTE_ALIGN_CEIL(val, align) \
307 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
310 * Macro to align a pointer to a given power-of-two. The resultant
311 * pointer will be a pointer of the same type as the first parameter, and
312 * point to an address no lower than the first parameter. Second parameter
313 * must be a power-of-two value.
314 * This function is the same as RTE_PTR_ALIGN_CEIL
316 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
319 * Macro to align a value to a given power-of-two. The resultant
320 * value will be of the same type as the first parameter, and
321 * will be no lower than the first parameter. Second parameter
322 * must be a power-of-two value.
323 * This function is the same as RTE_ALIGN_CEIL
325 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
328 * Macro to align a value to the multiple of given value. The resultant
329 * value will be of the same type as the first parameter and will be no lower
330 * than the first parameter.
332 #define RTE_ALIGN_MUL_CEIL(v, mul) \
333 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
336 * Macro to align a value to the multiple of given value. The resultant
337 * value will be of the same type as the first parameter and will be no higher
338 * than the first parameter.
340 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
341 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
344 * Macro to align value to the nearest multiple of the given value.
345 * The resultant value might be greater than or less than the first parameter
346 * whichever difference is the lowest.
348 #define RTE_ALIGN_MUL_NEAR(v, mul) \
350 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
351 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
352 (ceil - (v)) > ((v) - floor) ? floor : ceil; \
356 * Checks if a pointer is aligned to a given power-of-two value
359 * The pointer whose alignment is to be checked
361 * The power-of-two value to which the ptr should be aligned
364 * True(1) where the pointer is correctly aligned, false(0) otherwise
367 rte_is_aligned(void *ptr, unsigned align)
369 return RTE_PTR_ALIGN(ptr, align) == ptr;
372 /*********** Macros for compile type checks ********/
375 * Triggers an error at compilation time if the condition is true.
377 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
379 /*********** Cache line related macros ********/
381 /** Cache line mask. */
382 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
384 /** Return the first cache-aligned value greater or equal to size. */
385 #define RTE_CACHE_LINE_ROUNDUP(size) \
386 (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
387 RTE_CACHE_LINE_SIZE))
389 /** Cache line size in terms of log2 */
390 #if RTE_CACHE_LINE_SIZE == 64
391 #define RTE_CACHE_LINE_SIZE_LOG2 6
392 #elif RTE_CACHE_LINE_SIZE == 128
393 #define RTE_CACHE_LINE_SIZE_LOG2 7
395 #error "Unsupported cache line size"
398 /** Minimum Cache line size. */
399 #define RTE_CACHE_LINE_MIN_SIZE 64
401 /** Force alignment to cache line. */
402 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
404 /** Force minimum cache line alignment. */
405 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
407 /*********** PA/IOVA type definitions ********/
409 /** Physical address */
410 typedef uint64_t phys_addr_t;
411 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
414 * IO virtual address type.
415 * When the physical addressing mode (IOVA as PA) is in use,
416 * the translation from an IO virtual address (IOVA) to a physical address
417 * is a direct mapping, i.e. the same value.
418 * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
420 typedef uint64_t rte_iova_t;
421 #define RTE_BAD_IOVA ((rte_iova_t)-1)
423 /*********** Structure alignment markers ********/
425 /** Generic marker for any place in a structure. */
426 __extension__ typedef void *RTE_MARKER[0];
427 /** Marker for 1B alignment in a structure. */
428 __extension__ typedef uint8_t RTE_MARKER8[0];
429 /** Marker for 2B alignment in a structure. */
430 __extension__ typedef uint16_t RTE_MARKER16[0];
431 /** Marker for 4B alignment in a structure. */
432 __extension__ typedef uint32_t RTE_MARKER32[0];
433 /** Marker for 8B alignment in a structure. */
434 __extension__ typedef uint64_t RTE_MARKER64[0];
437 * Combines 32b inputs most significant set bits into the least
438 * significant bits to construct a value with the same MSBs as x
439 * but all 1's under it.
442 * The integer whose MSBs need to be combined with its LSBs
444 * The combined value.
446 static inline uint32_t
447 rte_combine32ms1b(uint32_t x)
459 * Combines 64b inputs most significant set bits into the least
460 * significant bits to construct a value with the same MSBs as x
461 * but all 1's under it.
464 * The integer whose MSBs need to be combined with its LSBs
466 * The combined value.
468 static inline uint64_t
469 rte_combine64ms1b(uint64_t v)
481 /*********** Macros to work with powers of 2 ********/
484 * Macro to return 1 if n is a power of 2, 0 otherwise
486 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
489 * Returns true if n is a power of 2
492 * @return 1 if true, 0 otherwise
495 rte_is_power_of_2(uint32_t n)
497 return n && !(n & (n - 1));
501 * Aligns input parameter to the next power of 2
504 * The integer value to align
507 * Input parameter aligned to the next power of 2
509 static inline uint32_t
510 rte_align32pow2(uint32_t x)
513 x = rte_combine32ms1b(x);
519 * Aligns input parameter to the previous power of 2
522 * The integer value to align
525 * Input parameter aligned to the previous power of 2
527 static inline uint32_t
528 rte_align32prevpow2(uint32_t x)
530 x = rte_combine32ms1b(x);
536 * Aligns 64b input parameter to the next power of 2
539 * The 64b value to align
542 * Input parameter aligned to the next power of 2
544 static inline uint64_t
545 rte_align64pow2(uint64_t v)
548 v = rte_combine64ms1b(v);
554 * Aligns 64b input parameter to the previous power of 2
557 * The 64b value to align
560 * Input parameter aligned to the previous power of 2
562 static inline uint64_t
563 rte_align64prevpow2(uint64_t v)
565 v = rte_combine64ms1b(v);
570 /*********** Macros for calculating min and max **********/
573 * Macro to return the minimum of two numbers
575 #define RTE_MIN(a, b) \
577 typeof (a) _a = (a); \
578 typeof (b) _b = (b); \
583 * Macro to return the maximum of two numbers
585 #define RTE_MAX(a, b) \
587 typeof (a) _a = (a); \
588 typeof (b) _b = (b); \
592 /*********** Other general functions / macros ********/
595 * Searches the input parameter for the least significant set bit
596 * (starting from zero).
597 * If a least significant 1 bit is found, its bit index is returned.
598 * If the content of the input parameter is zero, then the content of the return
599 * value is undefined.
601 * input parameter, should not be zero.
603 * least significant set bit in the input parameter.
605 static inline uint32_t
606 rte_bsf32(uint32_t v)
608 return (uint32_t)__builtin_ctz(v);
612 * Searches the input parameter for the least significant set bit
613 * (starting from zero). Safe version (checks for input parameter being zero).
615 * @warning ``pos`` must be a valid pointer. It is not checked!
618 * The input parameter.
620 * If ``v`` was not 0, this value will contain position of least significant
621 * bit within the input parameter.
623 * Returns 0 if ``v`` was 0, otherwise returns 1.
626 rte_bsf32_safe(uint32_t v, uint32_t *pos)
636 * Return the rounded-up log2 of a integer.
638 * @note Contrary to the logarithm mathematical operation,
639 * rte_log2_u32(0) == 0 and not -inf.
642 * The input parameter.
644 * The rounded-up log2 of the input, or 0 if the input is 0.
646 static inline uint32_t
647 rte_log2_u32(uint32_t v)
651 v = rte_align32pow2(v);
657 * Return the last (most-significant) bit set.
659 * @note The last (most significant) bit is at position 32.
660 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
663 * The input parameter.
665 * The last (most-significant) bit set, or 0 if the input is 0.
668 rte_fls_u32(uint32_t x)
670 return (x == 0) ? 0 : 32 - __builtin_clz(x);
674 * Searches the input parameter for the least significant set bit
675 * (starting from zero).
676 * If a least significant 1 bit is found, its bit index is returned.
677 * If the content of the input parameter is zero, then the content of the return
678 * value is undefined.
680 * input parameter, should not be zero.
682 * least significant set bit in the input parameter.
685 rte_bsf64(uint64_t v)
687 return (uint32_t)__builtin_ctzll(v);
691 * Searches the input parameter for the least significant set bit
692 * (starting from zero). Safe version (checks for input parameter being zero).
694 * @warning ``pos`` must be a valid pointer. It is not checked!
697 * The input parameter.
699 * If ``v`` was not 0, this value will contain position of least significant
700 * bit within the input parameter.
702 * Returns 0 if ``v`` was 0, otherwise returns 1.
705 rte_bsf64_safe(uint64_t v, uint32_t *pos)
715 * Return the last (most-significant) bit set.
717 * @note The last (most significant) bit is at position 64.
718 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
719 * rte_fls_u64(0x8000000000000000) = 64
722 * The input parameter.
724 * The last (most-significant) bit set, or 0 if the input is 0.
727 rte_fls_u64(uint64_t x)
729 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
733 * Return the rounded-up log2 of a 64-bit integer.
735 * @note Contrary to the logarithm mathematical operation,
736 * rte_log2_u64(0) == 0 and not -inf.
739 * The input parameter.
741 * The rounded-up log2 of the input, or 0 if the input is 0.
743 static inline uint32_t
744 rte_log2_u64(uint64_t v)
748 v = rte_align64pow2(v);
749 /* we checked for v being 0 already, so no undefined behavior */
754 /** Return the offset of a field in a structure. */
755 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
759 * Return pointer to the wrapping struct instance.
769 * struct child *x = obtain(...);
770 * struct wrapper *w = container_of(x, struct wrapper, c);
773 #define container_of(ptr, type, member) __extension__ ({ \
774 const typeof(((type *)0)->member) *_ptr = (ptr); \
775 __rte_unused type *_target_ptr = \
777 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
781 /** Swap two variables. */
782 #define RTE_SWAP(a, b) \
790 * Get the size of a field in a structure.
793 * The type of the structure.
795 * The field in the structure.
797 * The size of the field in the structure, in bytes.
799 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
801 #define _RTE_STR(x) #x
802 /** Take a macro value and get a string version of it */
803 #define RTE_STR(x) _RTE_STR(x)
806 * ISO C helpers to modify format strings using variadic macros.
807 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
808 * An empty %s argument is appended to avoid a dangling comma.
810 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
811 #define RTE_FMT_HEAD(fmt, ...) fmt
812 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
814 /** Mask value of type "tp" for the first "ln" bit set. */
815 #define RTE_LEN2MASK(ln, tp) \
816 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
818 /** Number of elements in the array. */
819 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
822 * Converts a numeric string to the equivalent uint64_t value.
823 * As well as straight number conversion, also recognises the suffixes
824 * k, m and g for kilobytes, megabytes and gigabytes respectively.
826 * If a negative number is passed in i.e. a string with the first non-black
827 * character being "-", zero is returned. Zero is also returned in the case of
828 * an error with the strtoull call in the function.
831 * String containing number to convert.
835 static inline uint64_t
836 rte_str_to_size(const char *str)
839 unsigned long long size;
841 while (isspace((int)*str))
847 size = strtoull(str, &endptr, 0);
852 endptr++; /* allow 1 space gap */
855 case 'G': case 'g': size *= 1024; /* fall-through */
856 case 'M': case 'm': size *= 1024; /* fall-through */
857 case 'K': case 'k': size *= 1024; /* fall-through */
865 * Function to terminate the application immediately, printing an error
866 * message and returning the exit_code back to the shell.
868 * This function never returns
871 * The exit code to be returned by the application
873 * The format string to be used for printing the message. This can include
874 * printf format characters which will be expanded using any further parameters
878 rte_exit(int exit_code, const char *format, ...)
879 __rte_format_printf(2, 3);