1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2019 Intel Corporation
11 * Generic, commonly-used macro and inline function definitions
19 //#include <rte_config.h>
21 /* OS specific include */
25 #define typeof __typeof__
32 /** C extension macro for environments lacking C11 features. */
33 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
34 #define RTE_STD_C11 __extension__
40 * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
41 * while a host application (like pmdinfogen) may have another compiler.
42 * RTE_CC_IS_GNU is true if the file is compiled with GCC,
43 * no matter it is a target or host application.
45 #define RTE_CC_IS_GNU 0
48 #elif defined __INTEL_COMPILER
50 #elif defined __GNUC__
53 #define RTE_CC_IS_GNU 1
56 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
63 #define __rte_aligned(a) __attribute__((__aligned__(a)))
65 #ifdef RTE_ARCH_STRICT_ALIGN
66 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
67 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
68 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
70 typedef uint64_t unaligned_uint64_t;
71 typedef uint32_t unaligned_uint32_t;
72 typedef uint16_t unaligned_uint16_t;
76 * Force a structure to be packed
78 #define __rte_packed __attribute__((__packed__))
80 /******* Macro to mark functions and fields scheduled for removal *****/
81 #define __rte_deprecated __attribute__((__deprecated__))
84 * Mark a function or variable to a weak reference.
86 #define __rte_weak __attribute__((__weak__))
89 * Force symbol to be generated even if it appears to be unused.
91 #define __rte_used __attribute__((used))
93 /*********** Macros to eliminate unused variable warnings ********/
96 * short definition to mark a function parameter unused
98 #define __rte_unused __attribute__((__unused__))
101 * definition to mark a variable or function parameter as used so
102 * as to avoid a compiler warning
104 #define RTE_SET_USED(x) (void)(x)
107 * Check format string and its arguments at compile-time.
109 * GCC on Windows assumes MS-specific format string by default,
110 * even if the underlying stdio implementation is ANSI-compliant,
111 * so this must be overridden.
114 #define __rte_format_printf(format_index, first_arg) \
115 __attribute__((format(gnu_printf, format_index, first_arg)))
117 #define __rte_format_printf(format_index, first_arg) \
118 __attribute__((format(printf, format_index, first_arg)))
121 #define RTE_PRIORITY_LOG 101
122 #define RTE_PRIORITY_BUS 110
123 #define RTE_PRIORITY_CLASS 120
124 #define RTE_PRIORITY_LAST 65535
126 #define RTE_PRIO(prio) \
127 RTE_PRIORITY_ ## prio
130 * Run function before main() with high priority.
133 * Constructor function.
135 * Priority number must be above 100.
136 * Lowest number is the first to run.
138 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
139 #define RTE_INIT_PRIO(func, prio) \
140 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
144 * Run function before main() with low priority.
146 * The constructor will be run after prioritized constructors.
149 * Constructor function.
151 #define RTE_INIT(func) \
152 RTE_INIT_PRIO(func, LAST)
155 * Run after main() with low priority.
158 * Destructor function name.
160 * Priority number must be above 100.
161 * Lowest number is the last to run.
163 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
164 #define RTE_FINI_PRIO(func, prio) \
165 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
169 * Run after main() with high priority.
171 * The destructor will be run *before* prioritized destructors.
174 * Destructor function name.
176 #define RTE_FINI(func) \
177 RTE_FINI_PRIO(func, LAST)
180 * Hint never returning function
182 #define __rte_noreturn __attribute__((noreturn))
185 * Force a function to be inlined
187 #define __rte_always_inline inline __attribute__((always_inline))
190 * Force a function to be noinlined
192 #define __rte_noinline __attribute__((noinline))
195 * Hint function in the hot path
197 #define __rte_hot __attribute__((hot))
200 * Hint function in the cold path
202 #define __rte_cold __attribute__((cold))
204 /*********** Macros for pointer arithmetic ********/
207 * add a byte-value offset to a pointer
209 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
212 * subtract a byte-value offset from a pointer
214 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
217 * get the difference between two pointer values, i.e. how far apart
218 * in bytes are the locations they point two. It is assumed that
219 * ptr1 is greater than ptr2.
221 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
224 * Workaround to cast a const field of a structure to non-const type.
226 #define RTE_CAST_FIELD(var, field, type) \
227 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
229 /*********** Macros/static functions for doing alignment ********/
233 * Macro to align a pointer to a given power-of-two. The resultant
234 * pointer will be a pointer of the same type as the first parameter, and
235 * point to an address no higher than the first parameter. Second parameter
236 * must be a power-of-two value.
238 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
239 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
242 * Macro to align a value to a given power-of-two. The resultant value
243 * will be of the same type as the first parameter, and will be no
244 * bigger than the first parameter. Second parameter must be a
245 * power-of-two value.
247 #define RTE_ALIGN_FLOOR(val, align) \
248 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
251 * Macro to align a pointer to a given power-of-two. The resultant
252 * pointer will be a pointer of the same type as the first parameter, and
253 * point to an address no lower than the first parameter. Second parameter
254 * must be a power-of-two value.
256 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
257 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
260 * Macro to align a value to a given power-of-two. The resultant value
261 * will be of the same type as the first parameter, and will be no lower
262 * than the first parameter. Second parameter must be a power-of-two
265 #define RTE_ALIGN_CEIL(val, align) \
266 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
269 * Macro to align a pointer to a given power-of-two. The resultant
270 * pointer will be a pointer of the same type as the first parameter, and
271 * point to an address no lower than the first parameter. Second parameter
272 * must be a power-of-two value.
273 * This function is the same as RTE_PTR_ALIGN_CEIL
275 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
278 * Macro to align a value to a given power-of-two. The resultant
279 * value will be of the same type as the first parameter, and
280 * will be no lower than the first parameter. Second parameter
281 * must be a power-of-two value.
282 * This function is the same as RTE_ALIGN_CEIL
284 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
287 * Macro to align a value to the multiple of given value. The resultant
288 * value will be of the same type as the first parameter and will be no lower
289 * than the first parameter.
291 #define RTE_ALIGN_MUL_CEIL(v, mul) \
292 (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
295 * Macro to align a value to the multiple of given value. The resultant
296 * value will be of the same type as the first parameter and will be no higher
297 * than the first parameter.
299 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
300 ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
303 * Macro to align value to the nearest multiple of the given value.
304 * The resultant value might be greater than or less than the first parameter
305 * whichever difference is the lowest.
307 #define RTE_ALIGN_MUL_NEAR(v, mul) \
309 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
310 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
311 (ceil - v) > (v - floor) ? floor : ceil; \
315 * Checks if a pointer is aligned to a given power-of-two value
318 * The pointer whose alignment is to be checked
320 * The power-of-two value to which the ptr should be aligned
323 * True(1) where the pointer is correctly aligned, false(0) otherwise
326 rte_is_aligned(void *ptr, unsigned align)
328 return RTE_PTR_ALIGN(ptr, align) == ptr;
331 /*********** Macros for compile type checks ********/
334 * Triggers an error at compilation time if the condition is true.
336 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
338 /*********** Cache line related macros ********/
340 /** Cache line mask. */
341 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
343 /** Return the first cache-aligned value greater or equal to size. */
344 #define RTE_CACHE_LINE_ROUNDUP(size) \
345 (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
346 RTE_CACHE_LINE_SIZE))
348 /** Cache line size in terms of log2 */
349 #if RTE_CACHE_LINE_SIZE == 64
350 #define RTE_CACHE_LINE_SIZE_LOG2 6
351 #elif RTE_CACHE_LINE_SIZE == 128
352 #define RTE_CACHE_LINE_SIZE_LOG2 7
354 #error "Unsupported cache line size"
357 /** Minimum Cache line size. */
358 #define RTE_CACHE_LINE_MIN_SIZE 64
360 /** Force alignment to cache line. */
361 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
363 /** Force minimum cache line alignment. */
364 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
366 /*********** PA/IOVA type definitions ********/
368 /** Physical address */
369 typedef uint64_t phys_addr_t;
370 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
373 * IO virtual address type.
374 * When the physical addressing mode (IOVA as PA) is in use,
375 * the translation from an IO virtual address (IOVA) to a physical address
376 * is a direct mapping, i.e. the same value.
377 * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
379 typedef uint64_t rte_iova_t;
380 #define RTE_BAD_IOVA ((rte_iova_t)-1)
382 /*********** Structure alignment markers ********/
384 /** Generic marker for any place in a structure. */
385 __extension__ typedef void *RTE_MARKER[0];
386 /** Marker for 1B alignment in a structure. */
387 __extension__ typedef uint8_t RTE_MARKER8[0];
388 /** Marker for 2B alignment in a structure. */
389 __extension__ typedef uint16_t RTE_MARKER16[0];
390 /** Marker for 4B alignment in a structure. */
391 __extension__ typedef uint32_t RTE_MARKER32[0];
392 /** Marker for 8B alignment in a structure. */
393 __extension__ typedef uint64_t RTE_MARKER64[0];
396 * Combines 32b inputs most significant set bits into the least
397 * significant bits to construct a value with the same MSBs as x
398 * but all 1's under it.
401 * The integer whose MSBs need to be combined with its LSBs
403 * The combined value.
405 static inline uint32_t
406 rte_combine32ms1b(register uint32_t x)
418 * Combines 64b inputs most significant set bits into the least
419 * significant bits to construct a value with the same MSBs as x
420 * but all 1's under it.
423 * The integer whose MSBs need to be combined with its LSBs
425 * The combined value.
427 static inline uint64_t
428 rte_combine64ms1b(register uint64_t v)
440 /*********** Macros to work with powers of 2 ********/
443 * Macro to return 1 if n is a power of 2, 0 otherwise
445 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
448 * Returns true if n is a power of 2
451 * @return 1 if true, 0 otherwise
454 rte_is_power_of_2(uint32_t n)
456 return n && !(n & (n - 1));
460 * Aligns input parameter to the next power of 2
463 * The integer value to align
466 * Input parameter aligned to the next power of 2
468 static inline uint32_t
469 rte_align32pow2(uint32_t x)
472 x = rte_combine32ms1b(x);
478 * Aligns input parameter to the previous power of 2
481 * The integer value to align
484 * Input parameter aligned to the previous power of 2
486 static inline uint32_t
487 rte_align32prevpow2(uint32_t x)
489 x = rte_combine32ms1b(x);
495 * Aligns 64b input parameter to the next power of 2
498 * The 64b value to align
501 * Input parameter aligned to the next power of 2
503 static inline uint64_t
504 rte_align64pow2(uint64_t v)
507 v = rte_combine64ms1b(v);
513 * Aligns 64b input parameter to the previous power of 2
516 * The 64b value to align
519 * Input parameter aligned to the previous power of 2
521 static inline uint64_t
522 rte_align64prevpow2(uint64_t v)
524 v = rte_combine64ms1b(v);
529 /*********** Macros for calculating min and max **********/
532 * Macro to return the minimum of two numbers
534 #define RTE_MIN(a, b) \
536 typeof (a) _a = (a); \
537 typeof (b) _b = (b); \
542 * Macro to return the maximum of two numbers
544 #define RTE_MAX(a, b) \
546 typeof (a) _a = (a); \
547 typeof (b) _b = (b); \
551 /*********** Other general functions / macros ********/
554 * Searches the input parameter for the least significant set bit
555 * (starting from zero).
556 * If a least significant 1 bit is found, its bit index is returned.
557 * If the content of the input parameter is zero, then the content of the return
558 * value is undefined.
560 * input parameter, should not be zero.
562 * least significant set bit in the input parameter.
564 static inline uint32_t
565 rte_bsf32(uint32_t v)
567 return (uint32_t)__builtin_ctz(v);
571 * Searches the input parameter for the least significant set bit
572 * (starting from zero). Safe version (checks for input parameter being zero).
574 * @warning ``pos`` must be a valid pointer. It is not checked!
577 * The input parameter.
579 * If ``v`` was not 0, this value will contain position of least significant
580 * bit within the input parameter.
582 * Returns 0 if ``v`` was 0, otherwise returns 1.
585 rte_bsf32_safe(uint64_t v, uint32_t *pos)
595 * Return the rounded-up log2 of a integer.
597 * @note Contrary to the logarithm mathematical operation,
598 * rte_log2_u32(0) == 0 and not -inf.
601 * The input parameter.
603 * The rounded-up log2 of the input, or 0 if the input is 0.
605 static inline uint32_t
606 rte_log2_u32(uint32_t v)
610 v = rte_align32pow2(v);
616 * Return the last (most-significant) bit set.
618 * @note The last (most significant) bit is at position 32.
619 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
622 * The input parameter.
624 * The last (most-significant) bit set, or 0 if the input is 0.
627 rte_fls_u32(uint32_t x)
629 return (x == 0) ? 0 : 32 - __builtin_clz(x);
633 * Searches the input parameter for the least significant set bit
634 * (starting from zero).
635 * If a least significant 1 bit is found, its bit index is returned.
636 * If the content of the input parameter is zero, then the content of the return
637 * value is undefined.
639 * input parameter, should not be zero.
641 * least significant set bit in the input parameter.
644 rte_bsf64(uint64_t v)
646 return (uint32_t)__builtin_ctzll(v);
650 * Searches the input parameter for the least significant set bit
651 * (starting from zero). Safe version (checks for input parameter being zero).
653 * @warning ``pos`` must be a valid pointer. It is not checked!
656 * The input parameter.
658 * If ``v`` was not 0, this value will contain position of least significant
659 * bit within the input parameter.
661 * Returns 0 if ``v`` was 0, otherwise returns 1.
664 rte_bsf64_safe(uint64_t v, uint32_t *pos)
674 * Return the last (most-significant) bit set.
676 * @note The last (most significant) bit is at position 64.
677 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
678 * rte_fls_u64(0x8000000000000000) = 64
681 * The input parameter.
683 * The last (most-significant) bit set, or 0 if the input is 0.
686 rte_fls_u64(uint64_t x)
688 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
692 * Return the rounded-up log2 of a 64-bit integer.
694 * @note Contrary to the logarithm mathematical operation,
695 * rte_log2_u64(0) == 0 and not -inf.
698 * The input parameter.
700 * The rounded-up log2 of the input, or 0 if the input is 0.
702 static inline uint32_t
703 rte_log2_u64(uint64_t v)
707 v = rte_align64pow2(v);
708 /* we checked for v being 0 already, so no undefined behavior */
713 /** Return the offset of a field in a structure. */
714 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
718 * Return pointer to the wrapping struct instance.
728 * struct child *x = obtain(...);
729 * struct wrapper *w = container_of(x, struct wrapper, c);
732 #define container_of(ptr, type, member) __extension__ ({ \
733 const typeof(((type *)0)->member) *_ptr = (ptr); \
734 __rte_unused type *_target_ptr = \
736 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
741 * Get the size of a field in a structure.
744 * The type of the structure.
746 * The field in the structure.
748 * The size of the field in the structure, in bytes.
750 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
752 #define _RTE_STR(x) #x
753 /** Take a macro value and get a string version of it */
754 #define RTE_STR(x) _RTE_STR(x)
757 * ISO C helpers to modify format strings using variadic macros.
758 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
759 * An empty %s argument is appended to avoid a dangling comma.
761 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
762 #define RTE_FMT_HEAD(fmt, ...) fmt
763 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
765 /** Mask value of type "tp" for the first "ln" bit set. */
766 #define RTE_LEN2MASK(ln, tp) \
767 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
769 /** Number of elements in the array. */
770 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
773 * Converts a numeric string to the equivalent uint64_t value.
774 * As well as straight number conversion, also recognises the suffixes
775 * k, m and g for kilobytes, megabytes and gigabytes respectively.
777 * If a negative number is passed in i.e. a string with the first non-black
778 * character being "-", zero is returned. Zero is also returned in the case of
779 * an error with the strtoull call in the function.
782 * String containing number to convert.
787 static inline uint64_t
788 rte_str_to_size(const char *str)
791 unsigned long long size;
793 while (isspace((int)*str))
799 size = strtoull(str, &endptr, 0);
804 endptr++; /* allow 1 space gap */
807 case 'G': case 'g': size *= 1024; /* fall-through */
808 case 'M': case 'm': size *= 1024; /* fall-through */
809 case 'K': case 'k': size *= 1024; /* fall-through */
818 * Function to terminate the application immediately, printing an error
819 * message and returning the exit_code back to the shell.
821 * This function never returns
824 * The exit code to be returned by the application
826 * The format string to be used for printing the message. This can include
827 * printf format characters which will be expanded using any further parameters
831 rte_exit(int exit_code, const char *format, ...)
832 __rte_format_printf(2, 3);