DPDK 23.11.0
Loading...
Searching...
No Matches
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#ifdef __cplusplus
16extern "C" {
17#endif
18
19#include <stdint.h>
20#include <limits.h>
21
22#include <rte_config.h>
23
24/* OS specific include */
25#include <rte_os.h>
26
27#ifndef RTE_TOOLCHAIN_MSVC
28#ifndef typeof
29#define typeof __typeof__
30#endif
31#endif
32
33#ifndef __cplusplus
34#ifndef asm
35#define asm __asm__
36#endif
37#endif
38
39#ifdef RTE_TOOLCHAIN_MSVC
40#define __extension__
41#endif
42
43/*
44 * RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
45 * while a host application (like pmdinfogen) may have another compiler.
46 * RTE_CC_IS_GNU is true if the file is compiled with GCC,
47 * no matter it is a target or host application.
48 */
49#define RTE_CC_IS_GNU 0
50#if defined __clang__
51#define RTE_CC_CLANG
52#elif defined __INTEL_COMPILER
53#define RTE_CC_ICC
54#elif defined __GNUC__
55#define RTE_CC_GCC
56#undef RTE_CC_IS_GNU
57#define RTE_CC_IS_GNU 1
58#endif
59#if RTE_CC_IS_GNU
60#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
61 __GNUC_PATCHLEVEL__)
62#endif
63
67#ifdef RTE_TOOLCHAIN_MSVC
68#define __rte_aligned(a)
69#else
70#define __rte_aligned(a) __attribute__((__aligned__(a)))
71#endif
72
73#ifdef RTE_ARCH_STRICT_ALIGN
74typedef uint64_t unaligned_uint64_t __rte_aligned(1);
75typedef uint32_t unaligned_uint32_t __rte_aligned(1);
76typedef uint16_t unaligned_uint16_t __rte_aligned(1);
77#else
78typedef uint64_t unaligned_uint64_t;
79typedef uint32_t unaligned_uint32_t;
80typedef uint16_t unaligned_uint16_t;
81#endif
82
86#ifdef RTE_TOOLCHAIN_MSVC
87#define __rte_packed
88#else
89#define __rte_packed __attribute__((__packed__))
90#endif
91
95#ifdef RTE_TOOLCHAIN_MSVC
96#define __rte_may_alias
97#else
98#define __rte_may_alias __attribute__((__may_alias__))
99#endif
100
101/******* Macro to mark functions and fields scheduled for removal *****/
102#ifdef RTE_TOOLCHAIN_MSVC
103#define __rte_deprecated
104#define __rte_deprecated_msg(msg)
105#else
106#define __rte_deprecated __attribute__((__deprecated__))
107#define __rte_deprecated_msg(msg) __attribute__((__deprecated__(msg)))
108#endif
109
113#if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
114#define RTE_PRAGMA(x) _Pragma(#x)
115#define RTE_PRAGMA_WARNING(w) RTE_PRAGMA(GCC warning #w)
116#define RTE_DEPRECATED(x) RTE_PRAGMA_WARNING(#x is deprecated)
117#else
118#define RTE_DEPRECATED(x)
119#endif
120
124#define __rte_weak __attribute__((__weak__))
125
129#ifdef RTE_TOOLCHAIN_MSVC
130#define __rte_used
131#else
132#define __rte_used __attribute__((used))
133#endif
134
135/*********** Macros to eliminate unused variable warnings ********/
136
140#ifdef RTE_TOOLCHAIN_MSVC
141#define __rte_unused
142#else
143#define __rte_unused __attribute__((__unused__))
144#endif
145
149#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
150#define __rte_restrict __restrict
151#else
152#define __rte_restrict restrict
153#endif
154
159#define RTE_SET_USED(x) (void)(x)
160
168#ifdef RTE_TOOLCHAIN_MSVC
169#define __rte_format_printf(format_index, first_arg)
170#else
171#if RTE_CC_IS_GNU
172#define __rte_format_printf(format_index, first_arg) \
173 __attribute__((format(gnu_printf, format_index, first_arg)))
174#else
175#define __rte_format_printf(format_index, first_arg) \
176 __attribute__((format(printf, format_index, first_arg)))
177#endif
178#endif
179
185#if defined(RTE_CC_GCC) || defined(RTE_CC_CLANG)
186#define __rte_alloc_size(...) \
187 __attribute__((alloc_size(__VA_ARGS__)))
188#else
189#define __rte_alloc_size(...)
190#endif
191
192#define RTE_PRIORITY_LOG 101
193#define RTE_PRIORITY_BUS 110
194#define RTE_PRIORITY_CLASS 120
195#define RTE_PRIORITY_LAST 65535
196
197#define RTE_PRIO(prio) \
198 RTE_PRIORITY_ ## prio
199
209#ifndef RTE_INIT_PRIO /* Allow to override from EAL */
210#ifndef RTE_TOOLCHAIN_MSVC
211#define RTE_INIT_PRIO(func, prio) \
212static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
213#else
214/* definition from the Microsoft CRT */
215typedef int(__cdecl *_PIFV)(void);
216
217#define CTOR_SECTION_LOG ".CRT$XIB"
218#define CTOR_SECTION_BUS ".CRT$XIC"
219#define CTOR_SECTION_CLASS ".CRT$XID"
220#define CTOR_SECTION_LAST ".CRT$XIY"
221
222#define CTOR_PRIORITY_TO_SECTION(priority) CTOR_SECTION_ ## priority
223
224#define RTE_INIT_PRIO(name, priority) \
225 static void name(void); \
226 static int __cdecl name ## _thunk(void) { name(); return 0; } \
227 __pragma(const_seg(CTOR_PRIORITY_TO_SECTION(priority))) \
228 __declspec(allocate(CTOR_PRIORITY_TO_SECTION(priority))) \
229 _PIFV name ## _pointer = &name ## _thunk; \
230 __pragma(const_seg()) \
231 static void name(void)
232#endif
233#endif
234
243#define RTE_INIT(func) \
244 RTE_INIT_PRIO(func, LAST)
245
255#ifndef RTE_FINI_PRIO /* Allow to override from EAL */
256#ifndef RTE_TOOLCHAIN_MSVC
257#define RTE_FINI_PRIO(func, prio) \
258static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
259#else
260#define DTOR_SECTION_LOG "mydtor$B"
261#define DTOR_SECTION_BUS "mydtor$C"
262#define DTOR_SECTION_CLASS "mydtor$D"
263#define DTOR_SECTION_LAST "mydtor$Y"
264
265#define DTOR_PRIORITY_TO_SECTION(priority) DTOR_SECTION_ ## priority
266
267#define RTE_FINI_PRIO(name, priority) \
268 static void name(void); \
269 __pragma(const_seg(DTOR_PRIORITY_TO_SECTION(priority))) \
270 __declspec(allocate(DTOR_PRIORITY_TO_SECTION(priority))) name ## _pointer = &name; \
271 __pragma(const_seg()) \
272 static void name(void)
273#endif
274#endif
275
284#define RTE_FINI(func) \
285 RTE_FINI_PRIO(func, LAST)
286
290#ifdef RTE_TOOLCHAIN_MSVC
291#define __rte_noreturn
292#else
293#define __rte_noreturn __attribute__((noreturn))
294#endif
295
319#ifdef RTE_TOOLCHAIN_MSVC
320#define __rte_warn_unused_result
321#else
322#define __rte_warn_unused_result __attribute__((warn_unused_result))
323#endif
324
328#ifdef RTE_TOOLCHAIN_MSVC
329#define __rte_always_inline
330#else
331#define __rte_always_inline inline __attribute__((always_inline))
332#endif
333
337#define __rte_noinline __attribute__((noinline))
338
342#define __rte_hot __attribute__((hot))
343
347#ifdef RTE_TOOLCHAIN_MSVC
348#define __rte_cold
349#else
350#define __rte_cold __attribute__((cold))
351#endif
352
356#ifdef RTE_MALLOC_ASAN
357#ifdef RTE_CC_CLANG
358#define __rte_no_asan __attribute__((no_sanitize("address", "hwaddress")))
359#else
360#define __rte_no_asan __attribute__((no_sanitize_address))
361#endif
362#else /* ! RTE_MALLOC_ASAN */
363#define __rte_no_asan
364#endif
365
366/*********** Macros for pointer arithmetic ********/
367
371#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
372
376#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
377
383#define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
384
388#define RTE_CAST_FIELD(var, field, type) \
389 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
390
391/*********** Macros/static functions for doing alignment ********/
392
393
400#define RTE_PTR_ALIGN_FLOOR(ptr, align) \
401 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)(ptr), align))
402
409#define RTE_ALIGN_FLOOR(val, align) \
410 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
411
418#define RTE_PTR_ALIGN_CEIL(ptr, align) \
419 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
420
427#define RTE_ALIGN_CEIL(val, align) \
428 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
429
437#define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
438
446#define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
447
453#define RTE_ALIGN_MUL_CEIL(v, mul) \
454 ((((v) + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
455
461#define RTE_ALIGN_MUL_FLOOR(v, mul) \
462 (((v) / ((typeof(v))(mul))) * (typeof(v))(mul))
463
469#define RTE_ALIGN_MUL_NEAR(v, mul) \
470 __extension__ ({ \
471 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
472 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
473 (ceil - (v)) > ((v) - floor) ? floor : ceil; \
474 })
475
487static inline int
488rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
489{
490 return ((uintptr_t)ptr & (align - 1)) == 0;
491}
492
493/*********** Macros for compile type checks ********/
494
498#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
499
500/*********** Cache line related macros ********/
501
503#define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
504
506#define RTE_CACHE_LINE_ROUNDUP(size) RTE_ALIGN_CEIL(size, RTE_CACHE_LINE_SIZE)
507
509#if RTE_CACHE_LINE_SIZE == 64
510#define RTE_CACHE_LINE_SIZE_LOG2 6
511#elif RTE_CACHE_LINE_SIZE == 128
512#define RTE_CACHE_LINE_SIZE_LOG2 7
513#else
514#error "Unsupported cache line size"
515#endif
516
518#define RTE_CACHE_LINE_MIN_SIZE 64
519
521#ifdef RTE_TOOLCHAIN_MSVC
522#define __rte_cache_aligned
523#else
524#define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
525#endif
526
528#define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
529
530#define _RTE_CACHE_GUARD_HELPER2(unique) \
531 char cache_guard_ ## unique[RTE_CACHE_LINE_SIZE * RTE_CACHE_GUARD_LINES] \
532 __rte_cache_aligned
533#define _RTE_CACHE_GUARD_HELPER1(unique) _RTE_CACHE_GUARD_HELPER2(unique)
541#define RTE_CACHE_GUARD _RTE_CACHE_GUARD_HELPER1(__COUNTER__)
542
543/*********** PA/IOVA type definitions ********/
544
546typedef uint64_t phys_addr_t;
547#define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
548
556typedef uint64_t rte_iova_t;
557#define RTE_BAD_IOVA ((rte_iova_t)-1)
558
559/*********** Structure alignment markers ********/
560
561#ifndef RTE_TOOLCHAIN_MSVC
562
564__extension__ typedef void *RTE_MARKER[0];
566__extension__ typedef uint8_t RTE_MARKER8[0];
568__extension__ typedef uint16_t RTE_MARKER16[0];
570__extension__ typedef uint32_t RTE_MARKER32[0];
572__extension__ typedef uint64_t RTE_MARKER64[0];
573
574#endif
575
576/*********** Macros for calculating min and max **********/
577
581#define RTE_MIN(a, b) \
582 __extension__ ({ \
583 typeof (a) _a = (a); \
584 typeof (b) _b = (b); \
585 _a < _b ? _a : _b; \
586 })
587
591#define RTE_MAX(a, b) \
592 __extension__ ({ \
593 typeof (a) _a = (a); \
594 typeof (b) _b = (b); \
595 _a > _b ? _a : _b; \
596 })
597
598/*********** Other general functions / macros ********/
599
600#ifndef offsetof
602#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
603#endif
604
619#ifndef container_of
620#ifdef RTE_TOOLCHAIN_MSVC
621#define container_of(ptr, type, member) \
622 ((type *)((uintptr_t)(ptr) - offsetof(type, member)))
623#else
624#define container_of(ptr, type, member) __extension__ ({ \
625 const typeof(((type *)0)->member) *_ptr = (ptr); \
626 __rte_unused type *_target_ptr = \
627 (type *)(ptr); \
628 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
629 })
630#endif
631#endif
632
634#define RTE_SWAP(a, b) \
635 __extension__ ({ \
636 typeof (a) _a = a; \
637 a = b; \
638 b = _a; \
639 })
640
651#define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
652
653#define _RTE_STR(x) #x
655#define RTE_STR(x) _RTE_STR(x)
656
662#define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
663#define RTE_FMT_HEAD(fmt, ...) fmt
664#define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
665
667#define RTE_LEN2MASK(ln, tp) \
668 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
669
671#define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
672
687uint64_t
688rte_str_to_size(const char *str);
689
704rte_exit(int exit_code, const char *format, ...)
706
707#ifdef __cplusplus
708}
709#endif
710
711#endif
uint64_t rte_str_to_size(const char *str)
__extension__ typedef uint64_t RTE_MARKER64[0]
Definition rte_common.h:572
__extension__ typedef uint8_t RTE_MARKER8[0]
Definition rte_common.h:566
__extension__ typedef uint32_t RTE_MARKER32[0]
Definition rte_common.h:570
__extension__ typedef uint16_t RTE_MARKER16[0]
Definition rte_common.h:568
#define __rte_format_printf(format_index, first_arg)
Definition rte_common.h:175
uint64_t rte_iova_t
Definition rte_common.h:556
#define __rte_aligned(a)
Definition rte_common.h:70
static int rte_is_aligned(const void *const __rte_restrict ptr, const unsigned int align)
Definition rte_common.h:488
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_noreturn
Definition rte_common.h:293
uint64_t phys_addr_t
Definition rte_common.h:546
__extension__ typedef void * RTE_MARKER[0]
Definition rte_common.h:564
#define __rte_restrict
Definition rte_common.h:150