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