Branch data Line data Source code
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 : : #include <assert.h>
16 : : #include <limits.h>
17 : : #include <stdbool.h>
18 : : #include <stdint.h>
19 : : #include <stdalign.h>
20 : :
21 : : #include <rte_compat.h>
22 : : #include <rte_config.h>
23 : :
24 : : /* OS specific include */
25 : : #include <rte_os.h>
26 : :
27 : : #ifdef __cplusplus
28 : : extern "C" {
29 : : #endif
30 : :
31 : : #ifndef RTE_TOOLCHAIN_MSVC
32 : : #ifndef typeof
33 : : #define typeof __typeof__
34 : : #endif
35 : : #endif
36 : :
37 : : #ifndef __cplusplus
38 : : #ifndef asm
39 : : #define asm __asm__
40 : : #endif
41 : : #endif
42 : :
43 : : #ifdef RTE_TOOLCHAIN_MSVC
44 : : #ifdef __cplusplus
45 : : #define __extension__
46 : : #endif
47 : : #endif
48 : :
49 : : /*
50 : : * Macro __rte_constant checks if an expression's value can be determined at
51 : : * compile time. It takes a single argument, the expression to test, and
52 : : * returns 1 if the expression is a compile-time constant, and 0 otherwise.
53 : : * For most compilers it uses built-in function __builtin_constant_p, but for
54 : : * MSVC it uses a different method because MSVC does not have an equivalent
55 : : * to __builtin_constant_p.
56 : : *
57 : : * The trick used with MSVC relies on the way null pointer constants interact
58 : : * with the type of a ?: expression:
59 : : * An integer constant expression with the value 0, or such an expression cast
60 : : * to type void *, is called a null pointer constant.
61 : : * If both the second and third operands (of the ?: expression) are pointers or
62 : : * one is a null pointer constant and the other is a pointer, the result type
63 : : * is a pointer to a type qualified with all the type qualifiers of the types
64 : : * referenced by both operands. Furthermore, if both operands are pointers to
65 : : * compatible types or to differently qualified versions of compatible types,
66 : : * the result type is a pointer to an appropriately qualified version of the
67 : : * composite type; if one operand is a null pointer constant, the result has
68 : : * the type of the other operand; otherwise, one operand is a pointer to void
69 : : * or a qualified version of void, in which case the result type is a pointer
70 : : * to an appropriately qualified version of void.
71 : : *
72 : : * The _Generic keyword then checks the type of the expression
73 : : * (void *) ((e) * 0ll). It matches this type against the types listed in the
74 : : * _Generic construct:
75 : : * - If the type is int *, the result is 1.
76 : : * - If the type is void *, the result is 0.
77 : : *
78 : : * This explanation with some more details can be found at:
79 : : * https://stackoverflow.com/questions/49480442/detecting-integer-constant-expressions-in-macros
80 : : */
81 : : #ifdef RTE_TOOLCHAIN_MSVC
82 : : #define __rte_constant(e) _Generic((1 ? (void *) ((e) * 0ll) : (int *) 0), int * : 1, void * : 0)
83 : : #else
84 : : #define __rte_constant(e) __extension__(__builtin_constant_p(e))
85 : : #endif
86 : :
87 : : /*
88 : : * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
89 : : * while a host application (like pmdinfogen) may have another compiler.
90 : : * RTE_CC_IS_GNU is true if the file is compiled with GCC,
91 : : * no matter it is a target or host application.
92 : : */
93 : : #define RTE_CC_IS_GNU 0
94 : : #if defined __clang__
95 : : #define RTE_CC_CLANG
96 : : #elif defined __GNUC__
97 : : #define RTE_CC_GCC
98 : : #undef RTE_CC_IS_GNU
99 : : #define RTE_CC_IS_GNU 1
100 : : #endif
101 : : #if RTE_CC_IS_GNU
102 : : #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
103 : : __GNUC_PATCHLEVEL__)
104 : : #endif
105 : :
106 : : /**
107 : : * Force type alignment
108 : : *
109 : : * This macro should be used when alignment of a struct or union type
110 : : * is required. For toolchain compatibility it should appear between
111 : : * the {struct,union} keyword and tag. e.g.
112 : : *
113 : : * struct __rte_aligned(8) tag { ... };
114 : : *
115 : : * If alignment of an object/variable is required then this macro should
116 : : * not be used, instead prefer C11 alignas(a).
117 : : */
118 : : #ifdef RTE_TOOLCHAIN_MSVC
119 : : #define __rte_aligned(a) __declspec(align(a))
120 : : #else
121 : : #define __rte_aligned(a) __attribute__((__aligned__(a)))
122 : : #endif
123 : :
124 : : #ifdef RTE_ARCH_STRICT_ALIGN
125 : : typedef uint64_t unaligned_uint64_t __rte_aligned(1);
126 : : typedef uint32_t unaligned_uint32_t __rte_aligned(1);
127 : : typedef uint16_t unaligned_uint16_t __rte_aligned(1);
128 : : #else
129 : : typedef uint64_t unaligned_uint64_t;
130 : : typedef uint32_t unaligned_uint32_t;
131 : : typedef uint16_t unaligned_uint16_t;
132 : : #endif
133 : :
134 : : /**
135 : : * @deprecated
136 : : * @see __rte_packed_begin
137 : : * @see __rte_packed_end
138 : : *
139 : : * Force a structure to be packed
140 : : */
141 : : #ifdef RTE_TOOLCHAIN_MSVC
142 : : #define __rte_packed RTE_DEPRECATED(__rte_packed)
143 : : #else
144 : : #define __rte_packed (RTE_DEPRECATED(__rte_packed) __attribute__((__packed__)))
145 : : #endif
146 : :
147 : : /**
148 : : * Force a structure to be packed
149 : : * Usage:
150 : : * struct __rte_packed_begin mystruct { ... } __rte_packed_end;
151 : : * union __rte_packed_begin myunion { ... } __rte_packed_end;
152 : : * Note: alignment attributes when present should precede __rte_packed_begin.
153 : : */
154 : : #ifdef RTE_TOOLCHAIN_MSVC
155 : : #define __rte_packed_begin __pragma(pack(push, 1))
156 : : #define __rte_packed_end __pragma(pack(pop))
157 : : #else
158 : : #define __rte_packed_begin
159 : : #define __rte_packed_end __attribute__((__packed__))
160 : : #endif
161 : :
162 : : /**
163 : : * Macro to mark a type that is not subject to type-based aliasing rules
164 : : */
165 : : #ifdef RTE_TOOLCHAIN_MSVC
166 : : #define __rte_may_alias
167 : : #else
168 : : #define __rte_may_alias __attribute__((__may_alias__))
169 : : #endif
170 : :
171 : : /******* Macro to mark functions and fields scheduled for removal *****/
172 : : #ifdef RTE_TOOLCHAIN_MSVC
173 : : #define __rte_deprecated
174 : : #define __rte_deprecated_msg(msg)
175 : : #else
176 : : #define __rte_deprecated __attribute__((__deprecated__))
177 : : #define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
178 : : #endif
179 : :
180 : : /**
181 : : * Macro to mark macros and defines scheduled for removal
182 : : */
183 : : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
184 : : #define RTE_PRAGMA(x) _Pragma(#x)
185 : : #define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
186 : : #define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
187 : : #else
188 : : #define RTE_DEPRECATED(x)
189 : : #endif
190 : :
191 : : /**
192 : : * Macros to cause the compiler to remember the state of the diagnostics as of
193 : : * each push, and restore to that point at each pop.
194 : : */
195 : : #if !defined(RTE_TOOLCHAIN_MSVC)
196 : : #define __rte_diagnostic_push _Pragma("GCC diagnostic push")
197 : : #define __rte_diagnostic_pop _Pragma("GCC diagnostic pop")
198 : : #else
199 : : #define __rte_diagnostic_push
200 : : #define __rte_diagnostic_pop
201 : : #endif
202 : :
203 : : /**
204 : : * Macro to disable compiler warnings about removing a type
205 : : * qualifier from the target type.
206 : : */
207 : : #if !defined(RTE_TOOLCHAIN_MSVC)
208 : : #define __rte_diagnostic_ignored_wcast_qual _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
209 : : #else
210 : : #define __rte_diagnostic_ignored_wcast_qual
211 : : #endif
212 : :
213 : : /**
214 : : * Mark a function or variable to a weak reference.
215 : : */
216 : : #ifdef RTE_TOOLCHAIN_MSVC
217 : : #define __rte_weak RTE_DEPRECATED(__rte_weak)
218 : : #else
219 : : #define __rte_weak RTE_DEPRECATED(__rte_weak) __attribute__((__weak__))
220 : : #endif
221 : :
222 : : /**
223 : : * Mark a function to be pure.
224 : : */
225 : : #ifdef RTE_TOOLCHAIN_MSVC
226 : : #define __rte_pure
227 : : #else
228 : : #define __rte_pure __attribute__((pure))
229 : : #endif
230 : :
231 : : /**
232 : : * Force symbol to be generated even if it appears to be unused.
233 : : */
234 : : #ifdef RTE_TOOLCHAIN_MSVC
235 : : #define __rte_used
236 : : #else
237 : : #define __rte_used __attribute__((used))
238 : : #endif
239 : :
240 : : /*********** Macros to eliminate unused variable warnings ********/
241 : :
242 : : /**
243 : : * short definition to mark a function parameter unused
244 : : */
245 : : #ifdef RTE_TOOLCHAIN_MSVC
246 : : #define __rte_unused
247 : : #else
248 : : #define __rte_unused __attribute__((__unused__))
249 : : #endif
250 : :
251 : : /**
252 : : * Mark pointer as restricted with regard to pointer aliasing.
253 : : */
254 : : #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
255 : : #define __rte_restrict __restrict
256 : : #else
257 : : #define __rte_restrict restrict
258 : : #endif
259 : :
260 : : /**
261 : : * definition to mark a variable or function parameter as used so
262 : : * as to avoid a compiler warning
263 : : */
264 : : #define RTE_SET_USED(x) (void)(x)
265 : :
266 : : /**
267 : : * Check format string and its arguments at compile-time.
268 : : *
269 : : * GCC on Windows assumes MS-specific format string by default,
270 : : * even if the underlying stdio implementation is ANSI-compliant,
271 : : * so this must be overridden.
272 : : */
273 : : #ifdef RTE_TOOLCHAIN_MSVC
274 : : #define __rte_format_printf(format_index, first_arg)
275 : : #else
276 : : #if RTE_CC_IS_GNU
277 : : #define __rte_format_printf(format_index, first_arg) \
278 : : __attribute__((format(gnu_printf, format_index, first_arg)))
279 : : #else
280 : : #define __rte_format_printf(format_index, first_arg) \
281 : : __attribute__((format(printf, format_index, first_arg)))
282 : : #endif
283 : : #endif
284 : :
285 : : /**
286 : : * Specify data or function section/segment.
287 : : */
288 : : #ifdef RTE_TOOLCHAIN_MSVC
289 : : #define __rte_section(name) \
290 : : __pragma(data_seg(name)) __declspec(allocate(name))
291 : : #else
292 : : #define __rte_section(name) \
293 : : __attribute__((section(name)))
294 : : #endif
295 : :
296 : : /**
297 : : * Tells compiler that the function returns a value that points to
298 : : * memory, where the size is given by the one or two arguments.
299 : : * Used by compiler to validate object size.
300 : : */
301 : : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
302 : : #define __rte_alloc_size(...) \
303 : : __attribute__((alloc_size(__VA_ARGS__)))
304 : : #else
305 : : #define __rte_alloc_size(...)
306 : : #endif
307 : :
308 : : /**
309 : : * Tells the compiler that the function returns a value that points to
310 : : * memory aligned by a function argument.
311 : : *
312 : : * Note: not enabled on Clang because it warns if align argument is zero.
313 : : */
314 : : #if defined(RTE_CC_GCC)
315 : : #define __rte_alloc_align(argno) \
316 : : __attribute__((alloc_align(argno)))
317 : : #else
318 : : #define __rte_alloc_align(argno)
319 : : #endif
320 : :
321 : : /**
322 : : * Tells the compiler this is a function like malloc and that the pointer
323 : : * returned cannot alias any other pointer (ie new memory).
324 : : */
325 : : #if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
326 : : #define __rte_malloc __attribute__((__malloc__))
327 : : #else
328 : : #define __rte_malloc
329 : : #endif
330 : :
331 : : /**
332 : : * With recent GCC versions also able to track that proper
333 : : * deallocator function is used for this pointer.
334 : : */
335 : : #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000)
336 : : #define __rte_dealloc(dealloc, argno) \
337 : : __attribute__((malloc(dealloc, argno)))
338 : : #else
339 : : #define __rte_dealloc(dealloc, argno)
340 : : #endif
341 : :
342 : : #define RTE_PRIORITY_LOG 101
343 : : #define RTE_PRIORITY_BUS 110
344 : : #define RTE_PRIORITY_CLASS 120
345 : : #define RTE_PRIORITY_LAST 65535
346 : :
347 : : #define RTE_PRIO(prio) \
348 : : RTE_PRIORITY_ ## prio
349 : :
350 : : /**
351 : : * Run function before main() with high priority.
352 : : *
353 : : * @param func
354 : : * Constructor function.
355 : : * @param prio
356 : : * Priority number must be above 100.
357 : : * Lowest number is the first to run.
358 : : */
359 : : #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
360 : : #ifndef RTE_TOOLCHAIN_MSVC
361 : : #define RTE_INIT_PRIO(func, prio) \
362 : : static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
363 : : #else
364 : : /* definition from the Microsoft CRT */
365 : : typedef int(__cdecl *_PIFV)(void);
366 : :
367 : : #define CTOR_SECTION_LOG ".CRT$XIB"
368 : : #define CTOR_SECTION_BUS ".CRT$XIC"
369 : : #define CTOR_SECTION_CLASS ".CRT$XID"
370 : : #define CTOR_SECTION_LAST ".CRT$XIY"
371 : :
372 : : #define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
373 : :
374 : : #define RTE_INIT_PRIO(name, priority) \
375 : : static void name(void); \
376 : : static int __cdecl name ## _thunk(void) { name(); return 0; } \
377 : : __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
378 : : __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
379 : : _PIFV name ## _pointer = &name ## _thunk; \
380 : : __pragma(const_seg()) \
381 : : static void name(void)
382 : : #endif
383 : : #endif
384 : :
385 : : /**
386 : : * Run function before main() with low priority.
387 : : *
388 : : * The constructor will be run after prioritized constructors.
389 : : *
390 : : * @param func
391 : : * Constructor function.
392 : : */
393 : : #define RTE_INIT(func) \
394 : : RTE_INIT_PRIO(func, LAST)
395 : :
396 : : /**
397 : : * Run after main() with low priority.
398 : : *
399 : : * @param func
400 : : * Destructor function name.
401 : : * @param prio
402 : : * Priority number must be above 100.
403 : : * Lowest number is the last to run.
404 : : */
405 : : #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
406 : : #ifndef RTE_TOOLCHAIN_MSVC
407 : : #define RTE_FINI_PRIO(func, prio) \
408 : : static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
409 : : #else
410 : : #define DTOR_SECTION_LOG "mydtor$B"
411 : : #define DTOR_SECTION_BUS "mydtor$C"
412 : : #define DTOR_SECTION_CLASS "mydtor$D"
413 : : #define DTOR_SECTION_LAST "mydtor$Y"
414 : :
415 : : #define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
416 : :
417 : : #define RTE_FINI_PRIO(name, priority) \
418 : : static void name(void); \
419 : : __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
420 : : __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) void *name ## _pointer = &name; \
421 : : __pragma(const_seg()) \
422 : : static void name(void)
423 : : #endif
424 : : #endif
425 : :
426 : : /**
427 : : * Run after main() with high priority.
428 : : *
429 : : * The destructor will be run *before* prioritized destructors.
430 : : *
431 : : * @param func
432 : : * Destructor function name.
433 : : */
434 : : #define RTE_FINI(func) \
435 : : RTE_FINI_PRIO(func, LAST)
436 : :
437 : : /**
438 : : * Hint never returning function
439 : : */
440 : : #ifdef RTE_TOOLCHAIN_MSVC
441 : : #define __rte_noreturn
442 : : #else
443 : : #define __rte_noreturn __attribute__((noreturn))
444 : : #endif
445 : :
446 : : /**
447 : : * Hint point in program never reached
448 : : */
449 : : #if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
450 : : #define __rte_unreachable() __extension__(__builtin_unreachable())
451 : : #else
452 : : #define __rte_unreachable() __assume(0)
453 : : #endif
454 : :
455 : : /**
456 : : * Issue a warning in case the function's return value is ignored.
457 : : *
458 : : * The use of this attribute should be restricted to cases where
459 : : * ignoring the marked function's return value is almost always a
460 : : * bug. With GCC, some effort is required to make clear that ignoring
461 : : * the return value is intentional. The usual void-casting method to
462 : : * mark something unused as used does not suppress the warning with
463 : : * this compiler.
464 : : *
465 : : * @code{.c}
466 : : * __rte_warn_unused_result int foo();
467 : : *
468 : : * void ignore_foo_result(void) {
469 : : * foo(); // generates a warning with all compilers
470 : : *
471 : : * (void)foo(); // still generates the warning with GCC (but not clang)
472 : : *
473 : : * int unused __rte_unused;
474 : : * unused = foo(); // does the trick with all compilers
475 : : * }
476 : : * @endcode
477 : : */
478 : : #ifdef RTE_TOOLCHAIN_MSVC
479 : : #define __rte_warn_unused_result
480 : : #else
481 : : #define __rte_warn_unused_result __attribute__((warn_unused_result))
482 : : #endif
483 : :
484 : : /**
485 : : * Force a function to be inlined
486 : : */
487 : : #ifdef RTE_TOOLCHAIN_MSVC
488 : : #define __rte_always_inline __forceinline
489 : : #else
490 : : #define __rte_always_inline inline __attribute__((always_inline))
491 : : #endif
492 : :
493 : : /**
494 : : * Force a function to be noinlined
495 : : */
496 : : #ifdef RTE_TOOLCHAIN_MSVC
497 : : #define __rte_noinline __declspec(noinline)
498 : : #else
499 : : #define __rte_noinline __attribute__((noinline))
500 : : #endif
501 : :
502 : : /**
503 : : * Hint function in the hot path
504 : : */
505 : : #ifdef RTE_TOOLCHAIN_MSVC
506 : : #define __rte_hot
507 : : #else
508 : : #define __rte_hot __attribute__((hot))
509 : : #endif
510 : :
511 : : /**
512 : : * Hint function in the cold path
513 : : */
514 : : #ifdef RTE_TOOLCHAIN_MSVC
515 : : #define __rte_cold
516 : : #else
517 : : #define __rte_cold __attribute__((cold))
518 : : #endif
519 : :
520 : : /**
521 : : * Hint precondition
522 : : *
523 : : * @warning Depending on the compiler, any code in ``condition`` might be executed.
524 : : * This currently only occurs with GCC prior to version 13.
525 : : */
526 : : #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 130000)
527 : : #define __rte_assume(condition) __attribute__((assume(condition)))
528 : : #elif defined(RTE_TOOLCHAIN_GCC)
529 : : #define __rte_assume(condition) do { if (!(condition)) __rte_unreachable(); } while (0)
530 : : #elif defined(RTE_TOOLCHAIN_CLANG)
531 : : #define __rte_assume(condition) __extension__(__builtin_assume(condition))
532 : : #else
533 : : #define __rte_assume(condition) __assume(condition)
534 : : #endif
535 : :
536 : : /**
537 : : * Disable AddressSanitizer on some code
538 : : */
539 : : #ifdef RTE_MALLOC_ASAN
540 : : #ifdef RTE_CC_CLANG
541 : : #define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
542 : : #else
543 : : #define __rte_no_asan __attribute__((no_sanitize_address))
544 : : #endif
545 : : #else /* ! RTE_MALLOC_ASAN */
546 : : #define __rte_no_asan
547 : : #endif
548 : :
549 : : /*********** Macros for pointer arithmetic ********/
550 : :
551 : : /**
552 : : * add a byte-value offset to a pointer
553 : : */
554 : : #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
555 : :
556 : : /**
557 : : * subtract a byte-value offset from a pointer
558 : : */
559 : : #define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
560 : :
561 : : /**
562 : : * get the difference between two pointer values, i.e. how far apart
563 : : * in bytes are the locations they point two. It is assumed that
564 : : * ptr1 is greater than ptr2.
565 : : */
566 : : #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
567 : :
568 : : /*********** Macros for casting pointers ********/
569 : :
570 : : /**
571 : : * Macro to discard qualifiers (such as const, volatile, restrict) from a pointer,
572 : : * without the compiler emitting a warning.
573 : : */
574 : : #define RTE_PTR_UNQUAL(X) ((void *)(uintptr_t)(X))
575 : :
576 : : /**
577 : : * Macro to cast a pointer to a specific type,
578 : : * without the compiler emitting a warning about discarding qualifiers.
579 : : *
580 : : * @warning
581 : : * When casting a pointer to point to a larger type, the resulting pointer may
582 : : * be misaligned, which results in undefined behavior.
583 : : * E.g.:
584 : : *
585 : : * struct s {
586 : : * uint16_t a;
587 : : * uint8_t b;
588 : : * uint8_t c;
589 : : * uint8_t d;
590 : : * } v;
591 : : * uint16_t * p = RTE_CAST_PTR(uint16_t *, &v.c); // "p" is not 16 bit aligned!
592 : : */
593 : : #define RTE_CAST_PTR(type, ptr) ((type)(uintptr_t)(ptr))
594 : :
595 : : /**
596 : : * Workaround to cast a const field of a structure to non-const type.
597 : : */
598 : : #define RTE_CAST_FIELD(var, field, type) \
599 : : (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
600 : :
601 : : /*********** Macros/static functions for doing alignment ********/
602 : :
603 : :
604 : : /**
605 : : * Macro to align a pointer to a given power-of-two. The resultant
606 : : * pointer will be a pointer of the same type as the first parameter, and
607 : : * point to an address no higher than the first parameter. Second parameter
608 : : * must be a power-of-two value.
609 : : */
610 : : #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
611 : : ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
612 : :
613 : : /**
614 : : * Macro to align a value to a given power-of-two. The resultant value
615 : : * will be of the same type as the first parameter, and will be no
616 : : * bigger than the first parameter. Second parameter must be a
617 : : * power-of-two value.
618 : : */
619 : : #define RTE_ALIGN_FLOOR(val, align) \
620 : : (typeof(val))((val) & (~((typeof(val))((align) - 1))))
621 : :
622 : : /**
623 : : * Macro to align a pointer to a given power-of-two. The resultant
624 : : * pointer will be a pointer of the same type as the first parameter, and
625 : : * point to an address no lower than the first parameter. Second parameter
626 : : * must be a power-of-two value.
627 : : */
628 : : #define RTE_PTR_ALIGN_CEIL(ptr, align) \
629 : : RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
630 : :
631 : : /**
632 : : * Macro to align a value to a given power-of-two. The resultant value
633 : : * will be of the same type as the first parameter, and will be no lower
634 : : * than the first parameter. Second parameter must be a power-of-two
635 : : * value.
636 : : */
637 : : #define RTE_ALIGN_CEIL(val, align) \
638 : : RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
639 : :
640 : : /**
641 : : * Macro to align a pointer to a given power-of-two. The resultant
642 : : * pointer will be a pointer of the same type as the first parameter, and
643 : : * point to an address no lower than the first parameter. Second parameter
644 : : * must be a power-of-two value.
645 : : * This function is the same as RTE_PTR_ALIGN_CEIL
646 : : */
647 : : #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
648 : :
649 : : /**
650 : : * Macro to align a value to a given power-of-two. The resultant
651 : : * value will be of the same type as the first parameter, and
652 : : * will be no lower than the first parameter. Second parameter
653 : : * must be a power-of-two value.
654 : : * This function is the same as RTE_ALIGN_CEIL
655 : : */
656 : : #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
657 : :
658 : : /**
659 : : * Macro to align a value to the multiple of given value. The resultant
660 : : * value will be of the same type as the first parameter and will be no lower
661 : : * than the first parameter.
662 : : */
663 : : #define RTE_ALIGN_MUL_CEIL(v, mul) \
664 : : ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
665 : :
666 : : /**
667 : : * Macro to align a value to the multiple of given value. The resultant
668 : : * value will be of the same type as the first parameter and will be no higher
669 : : * than the first parameter.
670 : : */
671 : : #define RTE_ALIGN_MUL_FLOOR(v, mul) \
672 : : (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
673 : :
674 : : /**
675 : : * Macro to align value to the nearest multiple of the given value.
676 : : * The resultant value might be greater than or less than the first parameter
677 : : * whichever difference is the lowest.
678 : : */
679 : : #define RTE_ALIGN_MUL_NEAR(v, mul) \
680 : : __extension__ ({ \
681 : : typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
682 : : typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
683 : : (ceil - (v)) > ((v) - floor) ? floor : ceil; \
684 : : })
685 : :
686 : : /**
687 : : * Checks if a pointer is aligned to a given power-of-two value
688 : : *
689 : : * @param ptr
690 : : * The pointer whose alignment is to be checked
691 : : * @param align
692 : : * The power-of-two value to which the ptr should be aligned
693 : : *
694 : : * @return
695 : : * True(1) where the pointer is correctly aligned, false(0) otherwise
696 : : */
697 : : static inline int
698 : : rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
699 : : {
700 [ + + # # : 2 : return ((uintptr_t)ptr & (align - 1)) == 0;
# # ]
701 : : }
702 : :
703 : : /*********** Macros for compile type checks ********/
704 : :
705 : : /* Workaround for toolchain issues with missing C11 macro in FreeBSD */
706 : : #if !defined(static_assert) && !defined(__cplusplus)
707 : : #define static_assert _Static_assert
708 : : #endif
709 : :
710 : : /**
711 : : * Triggers an error at compilation time if the condition is true.
712 : : *
713 : : * The do { } while(0) exists to workaround a bug in clang (#55821)
714 : : * where it would not handle _Static_assert in a switch case.
715 : : */
716 : : #define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
717 : :
718 : : /*********** Cache line related macros ********/
719 : :
720 : : /** Cache line mask. */
721 : : #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
722 : :
723 : : /** Return the first cache-aligned value greater or equal to size. */
724 : : #define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
725 : :
726 : : /** Cache line size in terms of log2 */
727 : : #if RTE_CACHE_LINE_SIZE == 64
728 : : #define RTE_CACHE_LINE_SIZE_LOG2 6
729 : : #elif RTE_CACHE_LINE_SIZE == 128
730 : : #define RTE_CACHE_LINE_SIZE_LOG2 7
731 : : #else
732 : : #error "Unsupported cache line size"
733 : : #endif
734 : :
735 : : /** Minimum Cache line size. */
736 : : #define RTE_CACHE_LINE_MIN_SIZE 64
737 : :
738 : : /** Force alignment to cache line. */
739 : : #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
740 : :
741 : : /** Force minimum cache line alignment. */
742 : : #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
743 : :
744 : : #define _RTE_CACHE_GUARD_HELPER2(unique) \
745 : : alignas(RTE_CACHE_LINE_SIZE) \
746 : : char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
747 : : #define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
748 : : /**
749 : : * Empty cache lines, to guard against false sharing-like effects
750 : : * on systems with a next-N-lines hardware prefetcher.
751 : : *
752 : : * Use as spacing between data accessed by different lcores,
753 : : * to prevent cache thrashing on hardware with speculative prefetching.
754 : : */
755 : : #define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
756 : :
757 : : /*********** PA/IOVA type definitions ********/
758 : :
759 : : /** Physical address */
760 : : typedef uint64_t phys_addr_t;
761 : : #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
762 : :
763 : : /**
764 : : * IO virtual address type.
765 : : * When the physical addressing mode (IOVA as PA) is in use,
766 : : * the translation from an IO virtual address (IOVA) to a physical address
767 : : * is a direct mapping, i.e. the same value.
768 : : * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
769 : : */
770 : : typedef uint64_t rte_iova_t;
771 : : #define RTE_BAD_IOVA ((rte_iova_t)-1)
772 : :
773 : : /*********** Structure alignment markers ********/
774 : :
775 : : #ifndef RTE_TOOLCHAIN_MSVC
776 : :
777 : : /** Generic marker for any place in a structure. */
778 : : __extension__ typedef void *RTE_MARKER[0];
779 : : /** Marker for 1B alignment in a structure. */
780 : : __extension__ typedef uint8_t RTE_MARKER8[0];
781 : : /** Marker for 2B alignment in a structure. */
782 : : __extension__ typedef uint16_t RTE_MARKER16[0];
783 : : /** Marker for 4B alignment in a structure. */
784 : : __extension__ typedef uint32_t RTE_MARKER32[0];
785 : : /** Marker for 8B alignment in a structure. */
786 : : __extension__ typedef uint64_t RTE_MARKER64[0];
787 : :
788 : : #endif
789 : :
790 : : /*********** Macros for calculating min and max **********/
791 : :
792 : : /**
793 : : * Macro to return the minimum of two numbers
794 : : */
795 : : #define RTE_MIN(a, b) \
796 : : __extension__ ({ \
797 : : typeof (a) _a = (a); \
798 : : typeof (b) _b = (b); \
799 : : _a < _b ? _a : _b; \
800 : : })
801 : :
802 : : /**
803 : : * Macro to return the minimum of two numbers
804 : : *
805 : : * As opposed to RTE_MIN, it does not use temporary variables so it is not safe
806 : : * if a or b is an expression. Yet it is guaranteed to be constant for use in
807 : : * static_assert().
808 : : */
809 : : #define RTE_MIN_T(a, b, t) \
810 : : ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
811 : :
812 : : /**
813 : : * Macro to return the maximum of two numbers
814 : : */
815 : : #define RTE_MAX(a, b) \
816 : : __extension__ ({ \
817 : : typeof (a) _a = (a); \
818 : : typeof (b) _b = (b); \
819 : : _a > _b ? _a : _b; \
820 : : })
821 : :
822 : : /**
823 : : * Macro to return the maximum of two numbers
824 : : *
825 : : * As opposed to RTE_MAX, it does not use temporary variables so it is not safe
826 : : * if a or b is an expression. Yet it is guaranteed to be constant for use in
827 : : * static_assert().
828 : : */
829 : : #define RTE_MAX_T(a, b, t) \
830 : : ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
831 : :
832 : : /*********** Other general functions / macros ********/
833 : :
834 : : #ifndef offsetof
835 : : /** Return the offset of a field in a structure. */
836 : : #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
837 : : #endif
838 : :
839 : : /**
840 : : * Return pointer to the wrapping struct instance.
841 : : *
842 : : * Example:
843 : : *
844 : : * struct wrapper {
845 : : * ...
846 : : * struct child c;
847 : : * ...
848 : : * };
849 : : *
850 : : * struct child *x = obtain(...);
851 : : * struct wrapper *w = container_of(x, struct wrapper, c);
852 : : */
853 : : #ifndef container_of
854 : : #ifdef RTE_TOOLCHAIN_MSVC
855 : : #define container_of(ptr, type, member) \
856 : : ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
857 : : #else
858 : : #define container_of(ptr, type, member) __extension__ ({ \
859 : : const typeof(((type *)0)->member) *_ptr = (ptr); \
860 : : __rte_unused type *_target_ptr = \
861 : : (type *)(ptr); \
862 : : (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
863 : : })
864 : : #endif
865 : : #endif
866 : :
867 : : /** Swap two variables. */
868 : : #define RTE_SWAP(a, b) \
869 : : __extension__ ({ \
870 : : typeof (a) _a = a; \
871 : : a = b; \
872 : : b = _a; \
873 : : })
874 : :
875 : : /**
876 : : * Get the size of a field in a structure.
877 : : *
878 : : * @param type
879 : : * The type of the structure.
880 : : * @param field
881 : : * The field in the structure.
882 : : * @return
883 : : * The size of the field in the structure, in bytes.
884 : : */
885 : : #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
886 : :
887 : : #define _RTE_STR(x) #x
888 : : /** Take a macro value and get a string version of it */
889 : : #define RTE_STR(x) _RTE_STR(x)
890 : :
891 : : /**
892 : : * ISO C helpers to modify format strings using variadic macros.
893 : : * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
894 : : * An empty %s argument is appended to avoid a dangling comma.
895 : : */
896 : : #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
897 : : #define RTE_FMT_HEAD(fmt, ...) fmt
898 : : #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
899 : :
900 : : /** Mask value of type "tp" for the first "ln" bit set. */
901 : : #define RTE_LEN2MASK(ln, tp) \
902 : : ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
903 : :
904 : : /** Number of elements in the array. */
905 : : #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
906 : :
907 : : /**
908 : : * Converts a numeric string to the equivalent uint64_t value.
909 : : * As well as straight number conversion, also recognises the suffixes
910 : : * k, m and g for kilobytes, megabytes and gigabytes respectively.
911 : : *
912 : : * If a negative number is passed in i.e. a string with the first non-black
913 : : * character being "-", zero is returned. Zero is also returned in the case of
914 : : * an error with the strtoull call in the function.
915 : : *
916 : : * @param str
917 : : * String containing number to convert.
918 : : * @return
919 : : * Number.
920 : : */
921 : : uint64_t
922 : : rte_str_to_size(const char *str);
923 : :
924 : : /**
925 : : * @warning
926 : : * @b EXPERIMENTAL: this API may change without prior notice.
927 : : *
928 : : * Converts the uint64_t value provided to a human-readable string.
929 : : * It null-terminates the string, truncating the data if needed.
930 : : * An optional unit (like "B") can be provided as a string. It will be
931 : : * appended to the number, and a space will be inserted before the unit if needed.
932 : : *
933 : : * Sample outputs: (1) "use_iec" disabled, (2) "use_iec" enabled,
934 : : * (3) "use_iec" enabled and "B" as unit.
935 : : * 0 : "0", "0", "0 B"
936 : : * 700 : "700", "700", "700 B"
937 : : * 1000 : "1.00 k", "1000", "1000 B"
938 : : * 1024 : "1.02 k", "1.00 ki", "1.00 kiB"
939 : : * 21474836480 : "21.5 G", "20.0 Gi", "20.0 GiB"
940 : : * 109951162777600 : "110 T", "100 Ti", "100 TiB"
941 : : *
942 : : * @param buf
943 : : * Buffer to write the string to.
944 : : * @param buf_size
945 : : * Size of the buffer.
946 : : * @param count
947 : : * Number to convert.
948 : : * @param use_iec
949 : : * If true, use IEC units (1024-based), otherwise use SI units (1000-based).
950 : : * @param unit
951 : : * Unit to append to the string (Like "B" for bytes). Can be NULL.
952 : : * @return
953 : : * buf on success, NULL if the buffer is too small.
954 : : */
955 : : __rte_experimental
956 : : char *
957 : : rte_size_to_str(char *buf, int buf_size, uint64_t count, bool use_iec, const char *unit);
958 : :
959 : : /**
960 : : * Function to terminate the application immediately, printing an error
961 : : * message and returning the exit_code back to the shell.
962 : : *
963 : : * This function never returns
964 : : *
965 : : * @param exit_code
966 : : * The exit code to be returned by the application
967 : : * @param format
968 : : * The format string to be used for printing the message. This can include
969 : : * printf format characters which will be expanded using any further parameters
970 : : * to the function.
971 : : */
972 : : __rte_noreturn void
973 : : rte_exit(int exit_code, const char *format, ...)
974 : : __rte_format_printf(2, 3);
975 : :
976 : : #ifdef __cplusplus
977 : : }
978 : : #endif
979 : :
980 : : #endif
|