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