DPDK 25.03.0-rc1
rte_common.h
Go to the documentation of this file.
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
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
26extern "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
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
91typedef uint64_t unaligned_uint64_t __rte_aligned(1);
92typedef uint32_t unaligned_uint32_t __rte_aligned(1);
93typedef uint16_t unaligned_uint16_t __rte_aligned(1);
94#else
95typedef uint64_t unaligned_uint64_t;
96typedef uint32_t unaligned_uint32_t;
97typedef uint16_t unaligned_uint16_t;
98#endif
99
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
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
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
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
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
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
182#define __rte_weak __attribute__((__weak__))
183
187#ifdef RTE_TOOLCHAIN_MSVC
188#define __rte_pure
189#else
190#define __rte_pure __attribute__((pure))
191#endif
192
196#ifdef RTE_TOOLCHAIN_MSVC
197#define __rte_used
198#else
199#define __rte_used __attribute__((used))
200#endif
201
202/*********** Macros to eliminate unused variable warnings ********/
203
207#ifdef RTE_TOOLCHAIN_MSVC
208#define __rte_unused
209#else
210#define __rte_unused __attribute__((__unused__))
211#endif
212
216#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
217#define __rte_restrict __restrict
218#else
219#define __rte_restrict restrict
220#endif
221
226#define RTE_SET_USED(x) (void)(x)
227
235#ifdef RTE_TOOLCHAIN_MSVC
236#define __rte_format_printf(format_index, first_arg)
237#else
238#if RTE_CC_IS_GNU
239#define __rte_format_printf(format_index, first_arg) \
240 __attribute__((format(gnu_printf, format_index, first_arg)))
241#else
242#define __rte_format_printf(format_index, first_arg) \
243 __attribute__((format(printf, format_index, first_arg)))
244#endif
245#endif
246
250#ifdef RTE_TOOLCHAIN_MSVC
251#define __rte_section(name) \
252 __pragma(data_seg(name)) __declspec(allocate(name))
253#else
254#define __rte_section(name) \
255 __attribute__((section(name)))
256#endif
257
263#if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
264#define __rte_alloc_size(...) \
265 __attribute__((alloc_size(__VA_ARGS__)))
266#else
267#define __rte_alloc_size(...)
268#endif
269
276#if defined(RTE_CC_GCC)
277#define __rte_alloc_align(argno) \
278 __attribute__((alloc_align(argno)))
279#else
280#define __rte_alloc_align(argno)
281#endif
282
287#if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
288#define __rte_malloc __attribute__((__malloc__))
289#else
290#define __rte_malloc
291#endif
292
297#if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 110000)
298#define __rte_dealloc(dealloc, argno) \
299 __attribute__((malloc(dealloc, argno)))
300#else
301#define __rte_dealloc(dealloc, argno)
302#endif
303
304#define RTE_PRIORITY_LOG 101
305#define RTE_PRIORITY_BUS 110
306#define RTE_PRIORITY_CLASS 120
307#define RTE_PRIORITY_LAST 65535
308
309#define RTE_PRIO(prio) \
310 RTE_PRIORITY_ ## prio
311
321#ifndef RTE_INIT_PRIO /* Allow to override from EAL */
322#ifndef RTE_TOOLCHAIN_MSVC
323#define RTE_INIT_PRIO(func, prio) \
324static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
325#else
326/* definition from the Microsoft CRT */
327typedef int(__cdecl *_PIFV)(void);
328
329#define CTOR_SECTION_LOG ".CRT$XIB"
330#define CTOR_SECTION_BUS ".CRT$XIC"
331#define CTOR_SECTION_CLASS ".CRT$XID"
332#define CTOR_SECTION_LAST ".CRT$XIY"
333
334#define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
335
336#define RTE_INIT_PRIO(name, priority) \
337 static void name(void); \
338 static int __cdecl name ## _thunk(void) { name(); return 0; } \
339 __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
340 __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
341 _PIFV name ## _pointer = &name ## _thunk; \
342 __pragma(const_seg()) \
343 static void name(void)
344#endif
345#endif
346
355#define RTE_INIT(func) \
356 RTE_INIT_PRIO(func, LAST)
357
367#ifndef RTE_FINI_PRIO /* Allow to override from EAL */
368#ifndef RTE_TOOLCHAIN_MSVC
369#define RTE_FINI_PRIO(func, prio) \
370static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
371#else
372#define DTOR_SECTION_LOG "mydtor$B"
373#define DTOR_SECTION_BUS "mydtor$C"
374#define DTOR_SECTION_CLASS "mydtor$D"
375#define DTOR_SECTION_LAST "mydtor$Y"
376
377#define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
378
379#define RTE_FINI_PRIO(name, priority) \
380 static void name(void); \
381 __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
382 __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) void *name ## _pointer = &name; \
383 __pragma(const_seg()) \
384 static void name(void)
385#endif
386#endif
387
396#define RTE_FINI(func) \
397 RTE_FINI_PRIO(func, LAST)
398
402#ifdef RTE_TOOLCHAIN_MSVC
403#define __rte_noreturn
404#else
405#define __rte_noreturn __attribute__((noreturn))
406#endif
407
411#if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
412#define __rte_unreachable() __extension__(__builtin_unreachable())
413#else
414#define __rte_unreachable() __assume(0)
415#endif
416
440#ifdef RTE_TOOLCHAIN_MSVC
441#define __rte_warn_unused_result
442#else
443#define __rte_warn_unused_result __attribute__((warn_unused_result))
444#endif
445
449#ifdef RTE_TOOLCHAIN_MSVC
450#define __rte_always_inline __forceinline
451#else
452#define __rte_always_inline inline __attribute__((always_inline))
453#endif
454
458#ifdef RTE_TOOLCHAIN_MSVC
459#define __rte_noinline __declspec(noinline)
460#else
461#define __rte_noinline __attribute__((noinline))
462#endif
463
467#ifdef RTE_TOOLCHAIN_MSVC
468#define __rte_hot
469#else
470#define __rte_hot __attribute__((hot))
471#endif
472
476#ifdef RTE_TOOLCHAIN_MSVC
477#define __rte_cold
478#else
479#define __rte_cold __attribute__((cold))
480#endif
481
488#if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 130000)
489#define __rte_assume(condition) __attribute__((assume(condition)))
490#elif defined(RTE_TOOLCHAIN_GCC)
491#define __rte_assume(condition) do { if (!(condition)) __rte_unreachable(); } while (0)
492#elif defined(RTE_TOOLCHAIN_CLANG)
493#define __rte_assume(condition) __extension__(__builtin_assume(condition))
494#else
495#define __rte_assume(condition) __assume(condition)
496#endif
497
501#ifdef RTE_MALLOC_ASAN
502#ifdef RTE_CC_CLANG
503#define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
504#else
505#define __rte_no_asan __attribute__((no_sanitize_address))
506#endif
507#else /* ! RTE_MALLOC_ASAN */
508#define __rte_no_asan
509#endif
510
511/*********** Macros for pointer arithmetic ********/
512
516#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
517
521#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
522
528#define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
529
530/*********** Macros for casting pointers ********/
531
536#define RTE_PTR_UNQUAL(X) ((void *)(uintptr_t)(X))
537
555#define RTE_CAST_PTR(type, ptr) ((type)(uintptr_t)(ptr))
556
560#define RTE_CAST_FIELD(var, field, type) \
561 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
562
563/*********** Macros/static functions for doing alignment ********/
564
565
572#define RTE_PTR_ALIGN_FLOOR(ptr, align) \
573 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
574
581#define RTE_ALIGN_FLOOR(val, align) \
582 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
583
590#define RTE_PTR_ALIGN_CEIL(ptr, align) \
591 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
592
599#define RTE_ALIGN_CEIL(val, align) \
600 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
601
609#define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
610
618#define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
619
625#define RTE_ALIGN_MUL_CEIL(v, mul) \
626 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
627
633#define RTE_ALIGN_MUL_FLOOR(v, mul) \
634 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
635
641#define RTE_ALIGN_MUL_NEAR(v, mul) \
642 __extension__ ({ \
643 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
644 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
645 (ceil - (v)) > ((v) - floor) ? floor : ceil; \
646 })
647
659static inline int
660rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
661{
662 return ((uintptr_t)ptr & (align - 1)) == 0;
663}
664
665/*********** Macros for compile type checks ********/
666
667/* Workaround for toolchain issues with missing C11 macro in FreeBSD */
668#if !defined(static_assert) && !defined(__cplusplus)
669#define static_assert _Static_assert
670#endif
671
678#define RTE_BUILD_BUG_ON(condition) do { static_assert(!(condition), #condition); } while (0)
679
680/*********** Cache line related macros ********/
681
683#define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
684
686#define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
687
689#if RTE_CACHE_LINE_SIZE == 64
690#define RTE_CACHE_LINE_SIZE_LOG2 6
691#elif RTE_CACHE_LINE_SIZE == 128
692#define RTE_CACHE_LINE_SIZE_LOG2 7
693#else
694#error "Unsupported cache line size"
695#endif
696
698#define RTE_CACHE_LINE_MIN_SIZE 64
699
701#define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
702
704#define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
705
706#define _RTE_CACHE_GUARD_HELPER2(unique) \
707 alignas(RTE_CACHE_LINE_SIZE) \
708 char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES]
709#define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
717#define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
718
719/*********** PA/IOVA type definitions ********/
720
722typedef uint64_t phys_addr_t;
723#define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
724
732typedef uint64_t rte_iova_t;
733#define RTE_BAD_IOVA ((rte_iova_t)-1)
734
735/*********** Structure alignment markers ********/
736
737#ifndef RTE_TOOLCHAIN_MSVC
738
740__extension__ typedef void *RTE_MARKER[0];
742__extension__ typedef uint8_t RTE_MARKER8[0];
744__extension__ typedef uint16_t RTE_MARKER16[0];
746__extension__ typedef uint32_t RTE_MARKER32[0];
748__extension__ typedef uint64_t RTE_MARKER64[0];
749
750#endif
751
752/*********** Macros for calculating min and max **********/
753
757#define RTE_MIN(a, b) \
758 __extension__ ({ \
759 typeof (a) _a = (a); \
760 typeof (b) _b = (b); \
761 _a < _b ? _a : _b; \
762 })
763
771#define RTE_MIN_T(a, b, t) \
772 ((t)(a) < (t)(b) ? (t)(a) : (t)(b))
773
777#define RTE_MAX(a, b) \
778 __extension__ ({ \
779 typeof (a) _a = (a); \
780 typeof (b) _b = (b); \
781 _a > _b ? _a : _b; \
782 })
783
791#define RTE_MAX_T(a, b, t) \
792 ((t)(a) > (t)(b) ? (t)(a) : (t)(b))
793
794/*********** Other general functions / macros ********/
795
796#ifndef offsetof
798#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
799#endif
800
815#ifndef container_of
816#ifdef RTE_TOOLCHAIN_MSVC
817#define container_of(ptr, type, member) \
818 ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
819#else
820#define container_of(ptr, type, member) __extension__ ({ \
821 const typeof(((type *)0)->member) *_ptr = (ptr); \
822 __rte_unused type *_target_ptr = \
823 (type *)(ptr); \
824 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
825 })
826#endif
827#endif
828
830#define RTE_SWAP(a, b) \
831 __extension__ ({ \
832 typeof (a) _a = a; \
833 a = b; \
834 b = _a; \
835 })
836
847#define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
848
849#define _RTE_STR(x) #x
851#define RTE_STR(x) _RTE_STR(x)
852
858#define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
859#define RTE_FMT_HEAD(fmt, ...) fmt
860#define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
861
863#define RTE_LEN2MASK(ln, tp) \
864 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
865
867#define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
868
883uint64_t
884rte_str_to_size(const char *str);
885
900rte_exit(int exit_code, const char *format, ...)
902
903#ifdef __cplusplus
904}
905#endif
906
907#endif
uint64_t rte_str_to_size(const char *str)
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition: rte_common.h:748
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition: rte_common.h:742
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition: rte_common.h:746
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition: rte_common.h:744
#define __rte_format_printf(format_index, first_arg)
Definition: rte_common.h:242
uint64_t rte_iova_t
Definition: rte_common.h:732
#define __rte_aligned(a)
Definition: rte_common.h:87
static int rte_is_aligned(const void *const __rte_restrict ptr, const unsigned int align)
Definition: rte_common.h:660
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_noreturn
Definition: rte_common.h:405
uint64_t phys_addr_t
Definition: rte_common.h:722
__extension__ typedef void * RTE_MARKER[0]
Definition: rte_common.h:740
#define __rte_restrict
Definition: rte_common.h:217