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 + \
69 #define __rte_aligned(a) __attribute__((__aligned__(a)))
71 #ifdef RTE_ARCH_STRICT_ALIGN
72 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
73 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
74 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
76 typedef uint64_t unaligned_uint64_t;
77 typedef uint32_t unaligned_uint32_t;
78 typedef uint16_t unaligned_uint16_t;
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__))
95 * Force symbol to be generated even if it appears to be unused.
97 #define __rte_used __attribute__((used))
99 /*********** Macros to eliminate unused variable warnings ********/
102 * short definition to mark a function parameter unused
104 #define __rte_unused __attribute__((__unused__))
107 * definition to mark a variable or function parameter as used so
108 * as to avoid a compiler warning
110 #define RTE_SET_USED(x) (void)(x)
113 * Check format string and its arguments at compile-time.
115 * GCC on Windows assumes MS-specific format string by default,
116 * even if the underlying stdio implementation is ANSI-compliant,
117 * so this must be overridden.
120 #define __rte_format_printf(format_index, first_arg) \
121 __attribute__((format(gnu_printf, format_index, first_arg)))
123 #define __rte_format_printf(format_index, first_arg) \
124 __attribute__((format(printf, format_index, first_arg)))
127 #define RTE_PRIORITY_LOG 101
128 #define RTE_PRIORITY_BUS 110
129 #define RTE_PRIORITY_CLASS 120
130 #define RTE_PRIORITY_LAST 65535
132 #define RTE_PRIO(prio) \
133 RTE_PRIORITY_ ## prio
136 * Run function before main() with high priority.
139 * Constructor function.
141 * Priority number must be above 100.
142 * Lowest number is the first to run.
144 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
145 #define RTE_INIT_PRIO(func, prio) \
146 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
150 * Run function before main() with low priority.
152 * The constructor will be run after prioritized constructors.
155 * Constructor function.
157 #define RTE_INIT(func) \
158 RTE_INIT_PRIO(func, LAST)
161 * Run after main() with low priority.
164 * Destructor function name.
166 * Priority number must be above 100.
167 * Lowest number is the last to run.
169 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
170 #define RTE_FINI_PRIO(func, prio) \
171 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
175 * Run after main() with high priority.
177 * The destructor will be run *before* prioritized destructors.
180 * Destructor function name.
182 #define RTE_FINI(func) \
183 RTE_FINI_PRIO(func, LAST)
186 * Hint never returning function
188 #define __rte_noreturn __attribute__((noreturn))
191 * Force a function to be inlined
193 #define __rte_always_inline inline __attribute__((always_inline))
196 * Force a function to be noinlined
198 #define __rte_noinline __attribute__((noinline))
201 * Hint function in the hot path
203 #define __rte_hot __attribute__((hot))
206 * Hint function in the cold path
208 #define __rte_cold __attribute__((cold))
210 /*********** Macros for pointer arithmetic ********/
213 * add a byte-value offset to a pointer
215 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
218 * subtract a byte-value offset from a pointer
220 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
223 * get the difference between two pointer values, i.e. how far apart
224 * in bytes are the locations they point two. It is assumed that
225 * ptr1 is greater than ptr2.
227 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
230 * Workaround to cast a const field of a structure to non-const type.
232 #define RTE_CAST_FIELD(var, field, type) \
233 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
235 /*********** Macros/static functions for doing alignment ********/
239 * Macro to align a pointer to a given power-of-two. The resultant
240 * pointer will be a pointer of the same type as the first parameter, and
241 * point to an address no higher than the first parameter. Second parameter
242 * must be a power-of-two value.
244 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
245 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
248 * Macro to align a value to a given power-of-two. The resultant value
249 * will be of the same type as the first parameter, and will be no
250 * bigger than the first parameter. Second parameter must be a
251 * power-of-two value.
253 #define RTE_ALIGN_FLOOR(val, align) \
254 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
257 * Macro to align a pointer to a given power-of-two. The resultant
258 * pointer will be a pointer of the same type as the first parameter, and
259 * point to an address no lower than the first parameter. Second parameter
260 * must be a power-of-two value.
262 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
263 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
266 * Macro to align a value to a given power-of-two. The resultant value
267 * will be of the same type as the first parameter, and will be no lower
268 * than the first parameter. Second parameter must be a power-of-two
271 #define RTE_ALIGN_CEIL(val, align) \
272 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
275 * Macro to align a pointer to a given power-of-two. The resultant
276 * pointer will be a pointer of the same type as the first parameter, and
277 * point to an address no lower than the first parameter. Second parameter
278 * must be a power-of-two value.
279 * This function is the same as RTE_PTR_ALIGN_CEIL
281 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
284 * Macro to align a value to a given power-of-two. The resultant
285 * value will be of the same type as the first parameter, and
286 * will be no lower than the first parameter. Second parameter
287 * must be a power-of-two value.
288 * This function is the same as RTE_ALIGN_CEIL
290 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
293 * Macro to align a value to the multiple of given value. The resultant
294 * value will be of the same type as the first parameter and will be no lower
295 * than the first parameter.
297 #define RTE_ALIGN_MUL_CEIL(v, mul) \
298 (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
301 * Macro to align a value to the multiple of given value. The resultant
302 * value will be of the same type as the first parameter and will be no higher
303 * than the first parameter.
305 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
306 ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
309 * Macro to align value to the nearest multiple of the given value.
310 * The resultant value might be greater than or less than the first parameter
311 * whichever difference is the lowest.
313 #define RTE_ALIGN_MUL_NEAR(v, mul) \
315 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
316 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
317 (ceil - v) > (v - floor) ? floor : ceil; \
321 * Checks if a pointer is aligned to a given power-of-two value
324 * The pointer whose alignment is to be checked
326 * The power-of-two value to which the ptr should be aligned
329 * True(1) where the pointer is correctly aligned, false(0) otherwise
332 rte_is_aligned(void *ptr, unsigned align)
334 return RTE_PTR_ALIGN(ptr, align) == ptr;
337 /*********** Macros for compile type checks ********/
340 * Triggers an error at compilation time if the condition is true.
342 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
344 /*********** Cache line related macros ********/
346 /** Cache line mask. */
347 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
349 /** Return the first cache-aligned value greater or equal to size. */
350 #define RTE_CACHE_LINE_ROUNDUP(size) \
351 (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
352 RTE_CACHE_LINE_SIZE))
354 /** Cache line size in terms of log2 */
355 #if RTE_CACHE_LINE_SIZE == 64
356 #define RTE_CACHE_LINE_SIZE_LOG2 6
357 #elif RTE_CACHE_LINE_SIZE == 128
358 #define RTE_CACHE_LINE_SIZE_LOG2 7
360 #error "Unsupported cache line size"
363 /** Minimum Cache line size. */
364 #define RTE_CACHE_LINE_MIN_SIZE 64
366 /** Force alignment to cache line. */
367 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
369 /** Force minimum cache line alignment. */
370 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
372 /*********** PA/IOVA type definitions ********/
374 /** Physical address */
375 typedef uint64_t phys_addr_t;
376 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
379 * IO virtual address type.
380 * When the physical addressing mode (IOVA as PA) is in use,
381 * the translation from an IO virtual address (IOVA) to a physical address
382 * is a direct mapping, i.e. the same value.
383 * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
385 typedef uint64_t rte_iova_t;
386 #define RTE_BAD_IOVA ((rte_iova_t)-1)
388 /*********** Structure alignment markers ********/
390 /** Generic marker for any place in a structure. */
391 __extension__ typedef void *RTE_MARKER[0];
392 /** Marker for 1B alignment in a structure. */
393 __extension__ typedef uint8_t RTE_MARKER8[0];
394 /** Marker for 2B alignment in a structure. */
395 __extension__ typedef uint16_t RTE_MARKER16[0];
396 /** Marker for 4B alignment in a structure. */
397 __extension__ typedef uint32_t RTE_MARKER32[0];
398 /** Marker for 8B alignment in a structure. */
399 __extension__ typedef uint64_t RTE_MARKER64[0];
402 * Combines 32b inputs most significant set bits into the least
403 * significant bits to construct a value with the same MSBs as x
404 * but all 1's under it.
407 * The integer whose MSBs need to be combined with its LSBs
409 * The combined value.
411 static inline uint32_t
412 rte_combine32ms1b(uint32_t x)
424 * Combines 64b inputs most significant set bits into the least
425 * significant bits to construct a value with the same MSBs as x
426 * but all 1's under it.
429 * The integer whose MSBs need to be combined with its LSBs
431 * The combined value.
433 static inline uint64_t
434 rte_combine64ms1b(uint64_t v)
446 /*********** Macros to work with powers of 2 ********/
449 * Macro to return 1 if n is a power of 2, 0 otherwise
451 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
454 * Returns true if n is a power of 2
457 * @return 1 if true, 0 otherwise
460 rte_is_power_of_2(uint32_t n)
462 return n && !(n & (n - 1));
466 * Aligns input parameter to the next power of 2
469 * The integer value to align
472 * Input parameter aligned to the next power of 2
474 static inline uint32_t
475 rte_align32pow2(uint32_t x)
478 x = rte_combine32ms1b(x);
484 * Aligns input parameter to the previous power of 2
487 * The integer value to align
490 * Input parameter aligned to the previous power of 2
492 static inline uint32_t
493 rte_align32prevpow2(uint32_t x)
495 x = rte_combine32ms1b(x);
501 * Aligns 64b input parameter to the next power of 2
504 * The 64b value to align
507 * Input parameter aligned to the next power of 2
509 static inline uint64_t
510 rte_align64pow2(uint64_t v)
513 v = rte_combine64ms1b(v);
519 * Aligns 64b input parameter to the previous power of 2
522 * The 64b value to align
525 * Input parameter aligned to the previous power of 2
527 static inline uint64_t
528 rte_align64prevpow2(uint64_t v)
530 v = rte_combine64ms1b(v);
535 /*********** Macros for calculating min and max **********/
538 * Macro to return the minimum of two numbers
540 #define RTE_MIN(a, b) \
542 typeof (a) _a = (a); \
543 typeof (b) _b = (b); \
548 * Macro to return the maximum of two numbers
550 #define RTE_MAX(a, b) \
552 typeof (a) _a = (a); \
553 typeof (b) _b = (b); \
557 /*********** Other general functions / macros ********/
560 * Searches the input parameter for the least significant set bit
561 * (starting from zero).
562 * If a least significant 1 bit is found, its bit index is returned.
563 * If the content of the input parameter is zero, then the content of the return
564 * value is undefined.
566 * input parameter, should not be zero.
568 * least significant set bit in the input parameter.
570 static inline uint32_t
571 rte_bsf32(uint32_t v)
573 return (uint32_t)__builtin_ctz(v);
577 * Searches the input parameter for the least significant set bit
578 * (starting from zero). Safe version (checks for input parameter being zero).
580 * @warning ``pos`` must be a valid pointer. It is not checked!
583 * The input parameter.
585 * If ``v`` was not 0, this value will contain position of least significant
586 * bit within the input parameter.
588 * Returns 0 if ``v`` was 0, otherwise returns 1.
591 rte_bsf32_safe(uint64_t v, uint32_t *pos)
601 * Return the rounded-up log2 of a integer.
603 * @note Contrary to the logarithm mathematical operation,
604 * rte_log2_u32(0) == 0 and not -inf.
607 * The input parameter.
609 * The rounded-up log2 of the input, or 0 if the input is 0.
611 static inline uint32_t
612 rte_log2_u32(uint32_t v)
616 v = rte_align32pow2(v);
622 * Return the last (most-significant) bit set.
624 * @note The last (most significant) bit is at position 32.
625 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
628 * The input parameter.
630 * The last (most-significant) bit set, or 0 if the input is 0.
633 rte_fls_u32(uint32_t x)
635 return (x == 0) ? 0 : 32 - __builtin_clz(x);
639 * Searches the input parameter for the least significant set bit
640 * (starting from zero).
641 * If a least significant 1 bit is found, its bit index is returned.
642 * If the content of the input parameter is zero, then the content of the return
643 * value is undefined.
645 * input parameter, should not be zero.
647 * least significant set bit in the input parameter.
650 rte_bsf64(uint64_t v)
652 return (uint32_t)__builtin_ctzll(v);
656 * Searches the input parameter for the least significant set bit
657 * (starting from zero). Safe version (checks for input parameter being zero).
659 * @warning ``pos`` must be a valid pointer. It is not checked!
662 * The input parameter.
664 * If ``v`` was not 0, this value will contain position of least significant
665 * bit within the input parameter.
667 * Returns 0 if ``v`` was 0, otherwise returns 1.
670 rte_bsf64_safe(uint64_t v, uint32_t *pos)
680 * Return the last (most-significant) bit set.
682 * @note The last (most significant) bit is at position 64.
683 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
684 * rte_fls_u64(0x8000000000000000) = 64
687 * The input parameter.
689 * The last (most-significant) bit set, or 0 if the input is 0.
692 rte_fls_u64(uint64_t x)
694 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
698 * Return the rounded-up log2 of a 64-bit integer.
700 * @note Contrary to the logarithm mathematical operation,
701 * rte_log2_u64(0) == 0 and not -inf.
704 * The input parameter.
706 * The rounded-up log2 of the input, or 0 if the input is 0.
708 static inline uint32_t
709 rte_log2_u64(uint64_t v)
713 v = rte_align64pow2(v);
714 /* we checked for v being 0 already, so no undefined behavior */
719 /** Return the offset of a field in a structure. */
720 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
724 * Return pointer to the wrapping struct instance.
734 * struct child *x = obtain(...);
735 * struct wrapper *w = container_of(x, struct wrapper, c);
738 #define container_of(ptr, type, member) __extension__ ({ \
739 const typeof(((type *)0)->member) *_ptr = (ptr); \
740 __rte_unused type *_target_ptr = \
742 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
747 * Get the size of a field in a structure.
750 * The type of the structure.
752 * The field in the structure.
754 * The size of the field in the structure, in bytes.
756 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
758 #define _RTE_STR(x) #x
759 /** Take a macro value and get a string version of it */
760 #define RTE_STR(x) _RTE_STR(x)
763 * ISO C helpers to modify format strings using variadic macros.
764 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
765 * An empty %s argument is appended to avoid a dangling comma.
767 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
768 #define RTE_FMT_HEAD(fmt, ...) fmt
769 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
771 /** Mask value of type "tp" for the first "ln" bit set. */
772 #define RTE_LEN2MASK(ln, tp) \
773 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
775 /** Number of elements in the array. */
776 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
779 * Converts a numeric string to the equivalent uint64_t value.
780 * As well as straight number conversion, also recognises the suffixes
781 * k, m and g for kilobytes, megabytes and gigabytes respectively.
783 * If a negative number is passed in i.e. a string with the first non-black
784 * character being "-", zero is returned. Zero is also returned in the case of
785 * an error with the strtoull call in the function.
788 * String containing number to convert.
792 static inline uint64_t
793 rte_str_to_size(const char *str)
796 unsigned long long size;
798 while (isspace((int)*str))
804 size = strtoull(str, &endptr, 0);
809 endptr++; /* allow 1 space gap */
812 case 'G': case 'g': size *= 1024; /* fall-through */
813 case 'M': case 'm': size *= 1024; /* fall-through */
814 case 'K': case 'k': size *= 1024; /* fall-through */
822 * Function to terminate the application immediately, printing an error
823 * message and returning the exit_code back to the shell.
825 * This function never returns
828 * The exit code to be returned by the application
830 * The format string to be used for printing the message. This can include
831 * printf format characters which will be expanded using any further parameters
835 rte_exit(int exit_code, const char *format, ...)
836 __rte_format_printf(2, 3);