xref: /dpdk/lib/eal/include/rte_common.h (revision 7253e3d2a5b6448b35dfd45e171566017239321b)
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 
8 /**
9  * @file
10  *
11  * Generic, commonly-used macro and inline function definitions
12  * for DPDK.
13  */
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #include <stdint.h>
20 #include <limits.h>
21 
22 #include <rte_config.h>
23 
24 /* OS specific include */
25 #include <rte_os.h>
26 
27 #ifndef typeof
28 #define typeof __typeof__
29 #endif
30 
31 #ifndef __cplusplus
32 #ifndef asm
33 #define asm __asm__
34 #endif
35 #endif
36 
37 /*
38  * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
39  * while a host application (like pmdinfogen) may have another compiler.
40  * RTE_CC_IS_GNU is true if the file is compiled with GCC,
41  * no matter it is a target or host application.
42  */
43 #define RTE_CC_IS_GNU 0
44 #if defined __clang__
45 #define RTE_CC_CLANG
46 #elif defined __INTEL_COMPILER
47 #define RTE_CC_ICC
48 #elif defined __GNUC__
49 #define RTE_CC_GCC
50 #undef RTE_CC_IS_GNU
51 #define RTE_CC_IS_GNU 1
52 #endif
53 #if RTE_CC_IS_GNU
54 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +	\
55 		__GNUC_PATCHLEVEL__)
56 #endif
57 
58 /**
59  * Force alignment
60  */
61 #define __rte_aligned(a) __attribute__((__aligned__(a)))
62 
63 #ifdef RTE_ARCH_STRICT_ALIGN
64 typedef uint64_t unaligned_uint64_t __rte_aligned(1);
65 typedef uint32_t unaligned_uint32_t __rte_aligned(1);
66 typedef uint16_t unaligned_uint16_t __rte_aligned(1);
67 #else
68 typedef uint64_t unaligned_uint64_t;
69 typedef uint32_t unaligned_uint32_t;
70 typedef uint16_t unaligned_uint16_t;
71 #endif
72 
73 /**
74  * Force a structure to be packed
75  */
76 #define __rte_packed __attribute__((__packed__))
77 
78 /**
79  * Macro to mark a type that is not subject to type-based aliasing rules
80  */
81 #define __rte_may_alias __attribute__((__may_alias__))
82 
83 /******* Macro to mark functions and fields scheduled for removal *****/
84 #define __rte_deprecated	__attribute__((__deprecated__))
85 #define __rte_deprecated_msg(msg)	__attribute__((__deprecated__(msg)))
86 
87 /**
88  *  Macro to mark macros and defines scheduled for removal
89  */
90 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
91 #define RTE_PRAGMA(x)  _Pragma(#x)
92 #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
93 #define RTE_DEPRECATED(x)  RTE_PRAGMA_WARNING(#x is deprecated)
94 #else
95 #define RTE_DEPRECATED(x)
96 #endif
97 
98 /**
99  * Mark a function or variable to a weak reference.
100  */
101 #define __rte_weak __attribute__((__weak__))
102 
103 /**
104  * Force symbol to be generated even if it appears to be unused.
105  */
106 #define __rte_used __attribute__((used))
107 
108 /*********** Macros to eliminate unused variable warnings ********/
109 
110 /**
111  * short definition to mark a function parameter unused
112  */
113 #define __rte_unused __attribute__((__unused__))
114 
115 /**
116  * Mark pointer as restricted with regard to pointer aliasing.
117  */
118 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
119 #define __rte_restrict __restrict
120 #else
121 #define __rte_restrict restrict
122 #endif
123 
124 /**
125  * definition to mark a variable or function parameter as used so
126  * as to avoid a compiler warning
127  */
128 #define RTE_SET_USED(x) (void)(x)
129 
130 /**
131  * Check format string and its arguments at compile-time.
132  *
133  * GCC on Windows assumes MS-specific format string by default,
134  * even if the underlying stdio implementation is ANSI-compliant,
135  * so this must be overridden.
136  */
137 #if RTE_CC_IS_GNU
138 #define __rte_format_printf(format_index, first_arg) \
139 	__attribute__((format(gnu_printf, format_index, first_arg)))
140 #else
141 #define __rte_format_printf(format_index, first_arg) \
142 	__attribute__((format(printf, format_index, first_arg)))
143 #endif
144 
145 /**
146  * Tells compiler that the function returns a value that points to
147  * memory, where the size is given by the one or two arguments.
148  * Used by compiler to validate object size.
149  */
150 #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
151 #define __rte_alloc_size(...) \
152 	__attribute__((alloc_size(__VA_ARGS__)))
153 #else
154 #define __rte_alloc_size(...)
155 #endif
156 
157 #define RTE_PRIORITY_LOG 101
158 #define RTE_PRIORITY_BUS 110
159 #define RTE_PRIORITY_CLASS 120
160 #define RTE_PRIORITY_LAST 65535
161 
162 #define RTE_PRIO(prio) \
163 	RTE_PRIORITY_ ## prio
164 
165 /**
166  * Run function before main() with high priority.
167  *
168  * @param func
169  *   Constructor function.
170  * @param prio
171  *   Priority number must be above 100.
172  *   Lowest number is the first to run.
173  */
174 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
175 #define RTE_INIT_PRIO(func, prio) \
176 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
177 #endif
178 
179 /**
180  * Run function before main() with low priority.
181  *
182  * The constructor will be run after prioritized constructors.
183  *
184  * @param func
185  *   Constructor function.
186  */
187 #define RTE_INIT(func) \
188 	RTE_INIT_PRIO(func, LAST)
189 
190 /**
191  * Run after main() with low priority.
192  *
193  * @param func
194  *   Destructor function name.
195  * @param prio
196  *   Priority number must be above 100.
197  *   Lowest number is the last to run.
198  */
199 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
200 #define RTE_FINI_PRIO(func, prio) \
201 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
202 #endif
203 
204 /**
205  * Run after main() with high priority.
206  *
207  * The destructor will be run *before* prioritized destructors.
208  *
209  * @param func
210  *   Destructor function name.
211  */
212 #define RTE_FINI(func) \
213 	RTE_FINI_PRIO(func, LAST)
214 
215 /**
216  * Hint never returning function
217  */
218 #define __rte_noreturn __attribute__((noreturn))
219 
220 /**
221  * Issue a warning in case the function's return value is ignored.
222  *
223  * The use of this attribute should be restricted to cases where
224  * ignoring the marked function's return value is almost always a
225  * bug. With GCC, some effort is required to make clear that ignoring
226  * the return value is intentional. The usual void-casting method to
227  * mark something unused as used does not suppress the warning with
228  * this compiler.
229  *
230  * @code{.c}
231  * __rte_warn_unused_result int foo();
232  *
233  * void ignore_foo_result(void) {
234  *         foo(); // generates a warning with all compilers
235  *
236  *         (void)foo(); // still generates the warning with GCC (but not clang)
237  *
238  *         int unused __rte_unused;
239  *         unused = foo(); // does the trick with all compilers
240  *  }
241  * @endcode
242  */
243 #define __rte_warn_unused_result __attribute__((warn_unused_result))
244 
245 /**
246  * Force a function to be inlined
247  */
248 #define __rte_always_inline inline __attribute__((always_inline))
249 
250 /**
251  * Force a function to be noinlined
252  */
253 #define __rte_noinline __attribute__((noinline))
254 
255 /**
256  * Hint function in the hot path
257  */
258 #define __rte_hot __attribute__((hot))
259 
260 /**
261  * Hint function in the cold path
262  */
263 #define __rte_cold __attribute__((cold))
264 
265 /**
266  * Disable AddressSanitizer on some code
267  */
268 #ifdef RTE_MALLOC_ASAN
269 #ifdef RTE_CC_CLANG
270 #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
271 #else
272 #define __rte_no_asan __attribute__((no_sanitize_address))
273 #endif
274 #else /* ! RTE_MALLOC_ASAN */
275 #define __rte_no_asan
276 #endif
277 
278 /*********** Macros for pointer arithmetic ********/
279 
280 /**
281  * add a byte-value offset to a pointer
282  */
283 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
284 
285 /**
286  * subtract a byte-value offset from a pointer
287  */
288 #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
289 
290 /**
291  * get the difference between two pointer values, i.e. how far apart
292  * in bytes are the locations they point two. It is assumed that
293  * ptr1 is greater than ptr2.
294  */
295 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
296 
297 /**
298  * Workaround to cast a const field of a structure to non-const type.
299  */
300 #define RTE_CAST_FIELD(var, field, type) \
301 	(*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
302 
303 /*********** Macros/static functions for doing alignment ********/
304 
305 
306 /**
307  * Macro to align a pointer to a given power-of-two. The resultant
308  * pointer will be a pointer of the same type as the first parameter, and
309  * point to an address no higher than the first parameter. Second parameter
310  * must be a power-of-two value.
311  */
312 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
313 	((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
314 
315 /**
316  * Macro to align a value to a given power-of-two. The resultant value
317  * will be of the same type as the first parameter, and will be no
318  * bigger than the first parameter. Second parameter must be a
319  * power-of-two value.
320  */
321 #define RTE_ALIGN_FLOOR(val, align) \
322 	(typeof(val))((val) & (~((typeof(val))((align) - 1))))
323 
324 /**
325  * Macro to align a pointer to a given power-of-two. The resultant
326  * pointer will be a pointer of the same type as the first parameter, and
327  * point to an address no lower than the first parameter. Second parameter
328  * must be a power-of-two value.
329  */
330 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
331 	RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
332 
333 /**
334  * Macro to align a value to a given power-of-two. The resultant value
335  * will be of the same type as the first parameter, and will be no lower
336  * than the first parameter. Second parameter must be a power-of-two
337  * value.
338  */
339 #define RTE_ALIGN_CEIL(val, align) \
340 	RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
341 
342 /**
343  * Macro to align a pointer to a given power-of-two. The resultant
344  * pointer will be a pointer of the same type as the first parameter, and
345  * point to an address no lower than the first parameter. Second parameter
346  * must be a power-of-two value.
347  * This function is the same as RTE_PTR_ALIGN_CEIL
348  */
349 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
350 
351 /**
352  * Macro to align a value to a given power-of-two. The resultant
353  * value will be of the same type as the first parameter, and
354  * will be no lower than the first parameter. Second parameter
355  * must be a power-of-two value.
356  * This function is the same as RTE_ALIGN_CEIL
357  */
358 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
359 
360 /**
361  * Macro to align a value to the multiple of given value. The resultant
362  * value will be of the same type as the first parameter and will be no lower
363  * than the first parameter.
364  */
365 #define RTE_ALIGN_MUL_CEIL(v, mul) \
366 	((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
367 
368 /**
369  * Macro to align a value to the multiple of given value. The resultant
370  * value will be of the same type as the first parameter and will be no higher
371  * than the first parameter.
372  */
373 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
374 	(((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
375 
376 /**
377  * Macro to align value to the nearest multiple of the given value.
378  * The resultant value might be greater than or less than the first parameter
379  * whichever difference is the lowest.
380  */
381 #define RTE_ALIGN_MUL_NEAR(v, mul)				\
382 	({							\
383 		typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);	\
384 		typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);	\
385 		(ceil - (v)) > ((v) - floor) ? floor : ceil;	\
386 	})
387 
388 /**
389  * Checks if a pointer is aligned to a given power-of-two value
390  *
391  * @param ptr
392  *   The pointer whose alignment is to be checked
393  * @param align
394  *   The power-of-two value to which the ptr should be aligned
395  *
396  * @return
397  *   True(1) where the pointer is correctly aligned, false(0) otherwise
398  */
399 static inline int
400 rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
401 {
402 	return ((uintptr_t)ptr & (align - 1)) == 0;
403 }
404 
405 /*********** Macros for compile type checks ********/
406 
407 /**
408  * Triggers an error at compilation time if the condition is true.
409  */
410 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
411 
412 /*********** Cache line related macros ********/
413 
414 /** Cache line mask. */
415 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
416 
417 /** Return the first cache-aligned value greater or equal to size. */
418 #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
419 
420 /** Cache line size in terms of log2 */
421 #if RTE_CACHE_LINE_SIZE == 64
422 #define RTE_CACHE_LINE_SIZE_LOG2 6
423 #elif RTE_CACHE_LINE_SIZE == 128
424 #define RTE_CACHE_LINE_SIZE_LOG2 7
425 #else
426 #error "Unsupported cache line size"
427 #endif
428 
429 /** Minimum Cache line size. */
430 #define RTE_CACHE_LINE_MIN_SIZE 64
431 
432 /** Force alignment to cache line. */
433 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
434 
435 /** Force minimum cache line alignment. */
436 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
437 
438 /*********** PA/IOVA type definitions ********/
439 
440 /** Physical address */
441 typedef uint64_t phys_addr_t;
442 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
443 
444 /**
445  * IO virtual address type.
446  * When the physical addressing mode (IOVA as PA) is in use,
447  * the translation from an IO virtual address (IOVA) to a physical address
448  * is a direct mapping, i.e. the same value.
449  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
450  */
451 typedef uint64_t rte_iova_t;
452 #define RTE_BAD_IOVA ((rte_iova_t)-1)
453 
454 /*********** Structure alignment markers ********/
455 
456 /** Generic marker for any place in a structure. */
457 __extension__ typedef void    *RTE_MARKER[0];
458 /** Marker for 1B alignment in a structure. */
459 __extension__ typedef uint8_t  RTE_MARKER8[0];
460 /** Marker for 2B alignment in a structure. */
461 __extension__ typedef uint16_t RTE_MARKER16[0];
462 /** Marker for 4B alignment in a structure. */
463 __extension__ typedef uint32_t RTE_MARKER32[0];
464 /** Marker for 8B alignment in a structure. */
465 __extension__ typedef uint64_t RTE_MARKER64[0];
466 
467 /*********** Macros for calculating min and max **********/
468 
469 /**
470  * Macro to return the minimum of two numbers
471  */
472 #define RTE_MIN(a, b) \
473 	__extension__ ({ \
474 		typeof (a) _a = (a); \
475 		typeof (b) _b = (b); \
476 		_a < _b ? _a : _b; \
477 	})
478 
479 /**
480  * Macro to return the maximum of two numbers
481  */
482 #define RTE_MAX(a, b) \
483 	__extension__ ({ \
484 		typeof (a) _a = (a); \
485 		typeof (b) _b = (b); \
486 		_a > _b ? _a : _b; \
487 	})
488 
489 /*********** Other general functions / macros ********/
490 
491 #ifndef offsetof
492 /** Return the offset of a field in a structure. */
493 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
494 #endif
495 
496 /**
497  * Return pointer to the wrapping struct instance.
498  *
499  * Example:
500  *
501  *  struct wrapper {
502  *      ...
503  *      struct child c;
504  *      ...
505  *  };
506  *
507  *  struct child *x = obtain(...);
508  *  struct wrapper *w = container_of(x, struct wrapper, c);
509  */
510 #ifndef container_of
511 #define container_of(ptr, type, member)	__extension__ ({		\
512 			const typeof(((type *)0)->member) *_ptr = (ptr); \
513 			__rte_unused type *_target_ptr =	\
514 				(type *)(ptr);				\
515 			(type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
516 		})
517 #endif
518 
519 /** Swap two variables. */
520 #define RTE_SWAP(a, b) \
521 	__extension__ ({ \
522 		typeof (a) _a = a; \
523 		a = b; \
524 		b = _a; \
525 	})
526 
527 /**
528  * Get the size of a field in a structure.
529  *
530  * @param type
531  *   The type of the structure.
532  * @param field
533  *   The field in the structure.
534  * @return
535  *   The size of the field in the structure, in bytes.
536  */
537 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
538 
539 #define _RTE_STR(x) #x
540 /** Take a macro value and get a string version of it */
541 #define RTE_STR(x) _RTE_STR(x)
542 
543 /**
544  * ISO C helpers to modify format strings using variadic macros.
545  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
546  * An empty %s argument is appended to avoid a dangling comma.
547  */
548 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
549 #define RTE_FMT_HEAD(fmt, ...) fmt
550 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
551 
552 /** Mask value of type "tp" for the first "ln" bit set. */
553 #define	RTE_LEN2MASK(ln, tp)	\
554 	((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
555 
556 /** Number of elements in the array. */
557 #define	RTE_DIM(a)	(sizeof (a) / sizeof ((a)[0]))
558 
559 /**
560  * Converts a numeric string to the equivalent uint64_t value.
561  * As well as straight number conversion, also recognises the suffixes
562  * k, m and g for kilobytes, megabytes and gigabytes respectively.
563  *
564  * If a negative number is passed in  i.e. a string with the first non-black
565  * character being "-", zero is returned. Zero is also returned in the case of
566  * an error with the strtoull call in the function.
567  *
568  * @param str
569  *     String containing number to convert.
570  * @return
571  *     Number.
572  */
573 uint64_t
574 rte_str_to_size(const char *str);
575 
576 /**
577  * Function to terminate the application immediately, printing an error
578  * message and returning the exit_code back to the shell.
579  *
580  * This function never returns
581  *
582  * @param exit_code
583  *     The exit code to be returned by the application
584  * @param format
585  *     The format string to be used for printing the message. This can include
586  *     printf format characters which will be expanded using any further parameters
587  *     to the function.
588  */
589 __rte_noreturn void
590 rte_exit(int exit_code, const char *format, ...)
591 	__rte_format_printf(2, 3);
592 
593 #ifdef __cplusplus
594 }
595 #endif
596 
597 #endif
598