12 #ifndef _CNE_COMMON_H_
13 #define _CNE_COMMON_H_
21 #include <sys/syscall.h>
22 #include <linux/version.h>
29 #define typeof __typeof__
36 #define CNE_VER_PREFIX "CNDP"
37 #define CNE_NAME_LEN 24
38 #define XDP_PKT_HEADROOM 256
39 #define CNE_PKTMBUF_HEADROOM (XDP_PKT_HEADROOM - sizeof(pktmbuf_t))
40 #define CNE_CACHE_LINE_SIZE 64
42 #define PKTDEV_QUEUE_STAT_CNTRS 16
43 #define CNE_MAX_QUEUES_PER_PORT 128
44 #define CNE_MAX_ETHPORTS 32
45 #define MEMPOOL_CACHE_MAX_SIZE 256
51 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0) && HAS_XSK_UMEM_SHARED == 1
52 #define CAN_USE_XSK_UMEM_SHARED 1
67 #define CNE_DEFAULT_SET(x, y, z) \
83 #define CNE_MAX_SET(x, z) \
90 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
91 #define CNE_STD_C11 __extension__
96 typedef uint64_t unaligned_uint64_t;
97 typedef uint32_t unaligned_uint32_t;
98 typedef uint16_t unaligned_uint16_t;
100 #define CNDP_API __attribute__((visibility("default")))
102 #ifdef CNE_BUILD_SHARED_LIBS
103 #define FUNCTION_VERSION(internal, api, ver) __asm__(".symver " #internal ", " #api "@" #ver)
104 #define DEFAULT_VERSION(internal, api, ver) __asm__(".symver " #internal ", " #api "@@" #ver)
106 #define FUNCTION_VERSION(internal, api, ver)
107 #define DEFAULT_VERSION(internal, api, ver)
116 #define cne_compiler_barrier() \
118 asm volatile("" : : : "memory"); \
124 #define __cne_aligned(a) __attribute__((aligned(a)))
129 #define __cne_packed __attribute__((__packed__))
132 #define __cne_deprecated __attribute__((__deprecated__))
137 #define __cne_weak __attribute__((__weak__))
144 #define __cne_unused __attribute__((__unused__))
150 #define CNE_SET_USED(x) (void)(x)
152 #define cne_roundup(_x, _y) ((((_x) + ((_y)-1)) / (_y)) * (_y))
153 #define cne_ctz(_v) __builtin_ctz(_v)
154 #define cne_prefixbits(_v) ((__typeof__(_v))(sizeof(_v) * 8) - cne_ctz(_v))
155 #define cne_numbytes(_v) ((cne_prefixbits(_v) + 7) / 8)
157 #define CNE_PRIORITY_INIT 101
158 #define CNE_PRIORITY_START 102
159 #define CNE_PRIORITY_THREAD 103
160 #define CNE_PRIORITY_STATE 110
161 #define CNE_PRIORITY_CLASS 120
162 #define CNE_PRIORITY_STACK 130
163 #define CNE_PRIORITY_LAST 65535
165 #define CNE_PRIO(prio) CNE_PRIORITY_##prio
176 #ifndef CNE_INIT_PRIO
177 #define CNE_INIT_PRIO(func, prio) \
178 static void __attribute__((constructor(CNE_PRIO(prio)), used)) func(void)
189 #define CNE_INIT(func) CNE_INIT_PRIO(func, LAST)
200 #ifndef CNE_FINI_PRIO
201 #define CNE_FINI_PRIO(func, prio) \
202 static void __attribute__((destructor(CNE_PRIO(prio)), used)) func(void)
213 #define CNE_FINI(func) CNE_FINI_PRIO(func, LAST)
218 #define __cne_always_inline inline __attribute__((__always_inline__))
223 #define __cne_noinline __attribute__((__noinline__))
228 #define CNE_PTR_CAST(ptr, x) (x)((uintptr_t)(ptr))
235 #define CNE_PTR_ADD(ptr, x) ((void *)((uintptr_t)(ptr) + (x)))
240 #define CNE_PTR_SUB(ptr, x) ((void *)((uintptr_t)ptr - (x)))
247 #define CNE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
252 #define CNE_CAST_FIELD(var, field, type) \
253 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
263 #define CNE_PTR_ALIGN_FLOOR(ptr, align) ((typeof(ptr))CNE_ALIGN_FLOOR((uintptr_t)ptr, align))
271 #define CNE_ALIGN_FLOOR(val, align) (typeof(val))((val) & (~((typeof(val))((align)-1))))
279 #define CNE_PTR_ALIGN_CEIL(ptr, align) \
280 CNE_PTR_ALIGN_FLOOR((typeof(ptr))CNE_PTR_ADD(ptr, (align)-1), align)
288 #define CNE_ALIGN_CEIL(val, align) CNE_ALIGN_FLOOR(((val) + ((typeof(val))(align)-1)), align)
297 #define CNE_PTR_ALIGN(ptr, align) CNE_PTR_ALIGN_CEIL(ptr, align)
306 #define CNE_ALIGN(val, align) CNE_ALIGN_CEIL(val, align)
313 #define CNE_ALIGN_MUL_CEIL(v, mul) \
314 (((v + (typeof(v))(mul)-1) / ((typeof(v))(mul))) * (typeof(v))(mul))
321 #define CNE_ALIGN_MUL_FLOOR(v, mul) ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
328 #define CNE_ALIGN_MUL_NEAR(v, mul) \
330 typeof(v) ceil = CNE_ALIGN_MUL_CEIL(v, mul); \
331 typeof(v) floor = CNE_ALIGN_MUL_FLOOR(v, mul); \
332 (ceil - v) > (v - floor) ? floor : ceil; \
357 #define CNE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
362 #define CNE_CACHE_LINE_MASK (CNE_CACHE_LINE_SIZE - 1)
365 #define CNE_CACHE_LINE_ROUNDUP(size) \
366 (CNE_CACHE_LINE_SIZE * ((size + CNE_CACHE_LINE_SIZE - 1) / CNE_CACHE_LINE_SIZE))
369 #if CNE_CACHE_LINE_SIZE == 64
370 #define CNE_CACHE_LINE_SIZE_LOG2 6
372 #error "Unsupported cache line size"
376 #define CNE_CACHE_LINE_MIN_SIZE 64
379 #define __cne_cache_aligned __cne_aligned(CNE_CACHE_LINE_SIZE)
382 #define __cne_cache_min_aligned __cne_aligned(CNE_CACHE_LINE_MIN_SIZE)
399 #define CNE_BAD_PHYS_ADDR ((phys_addr_t)-1)
409 #define CNE_BAD_IOVA ((cne_iova_t)-1)
421 static inline uint32_t
443 static inline uint64_t
461 #define CNE_IS_POWER_OF_2(n) ((n) && !(((n)-1) & (n)))
472 return n && !(n & (n - 1));
484 static inline uint32_t
502 static inline uint32_t
519 static inline uint64_t
537 static inline uint64_t
550 #define CNE_MIN(a, b) \
552 typeof(a) _a = (a); \
553 typeof(b) _b = (b); \
560 #define CNE_MAX(a, b) \
562 typeof(a) _a = (a); \
563 typeof(b) _b = (b); \
580 static inline uint32_t
583 return (uint32_t)__builtin_ctz(v);
618 static inline uint32_t
641 return (x == 0) ? 0 : 32 - __builtin_clz(x);
658 return (uint32_t)__builtin_ctzll(v);
700 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
711 static inline uint32_t
723 #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
741 #define container_of(ptr, type, member) \
743 const typeof(((type *)0)->member) *_ptr = (ptr); \
744 __attribute__((unused)) type *_target_ptr = (type *)(ptr); \
745 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
759 #define CNE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
761 #define _CNE_STR(x) #x
763 #define CNE_STR(x) _CNE_STR(x)
770 #define CNE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
771 #define CNE_FMT_HEAD(fmt, ...) fmt
772 #define CNE_FMT_TAIL(fmt, ...) __VA_ARGS__
775 #define CNE_LEN2MASK(ln, tp) ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
778 #define CNE_DIM(a) (int)(sizeof(a) / sizeof((a)[0]))
779 #define cne_countof(a) CNE_DIM(a)
795 static inline uint64_t
799 unsigned long long size;
801 while (isspace((
int)*str))
807 size = strtoull(str, &endptr, 0);
__extension__ typedef uint32_t CNE_MARKER32[0]
static uint64_t cne_combine64ms1b(uint64_t v)
static uint64_t cne_align64prevpow2(uint64_t v)
static int cne_bsf32_safe(uint64_t v, uint32_t *pos)
static uint32_t cne_log2_u32(uint32_t v)
__extension__ typedef uint8_t CNE_MARKER8[0]
static uint32_t cne_align32pow2(uint32_t x)
static uint64_t cne_align64pow2(uint64_t v)
static uint32_t cne_combine32ms1b(uint32_t x)
static int cne_is_aligned(void *ptr, unsigned align)
static int cne_fls_u32(uint32_t x)
static int cne_is_power_of_2(uint32_t n)
static uint32_t cne_log2_u64(uint64_t v)
__extension__ typedef uint16_t CNE_MARKER16[0]
static int cne_fls_u64(uint64_t x)
static uint64_t cne_str_to_size(const char *str)
__extension__ typedef void * CNE_MARKER[0]
static int cne_bsf64(uint64_t v)
static uint32_t cne_align32prevpow2(uint32_t x)
__extension__ typedef uint64_t CNE_MARKER64[0]
static uint32_t cne_bsf32(uint32_t v)
#define CNE_PTR_ALIGN(ptr, align)
static int cne_bsf64_safe(uint64_t v, uint32_t *pos)