CNDP  22.08.0
cne_common.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2019-2022 Intel Corporation
3  */
4 
12 #ifndef _CNE_COMMON_H_
13 #define _CNE_COMMON_H_
14 
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <unistd.h>
21 #include <sys/syscall.h>
22 #include <linux/version.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #ifndef typeof
29 #define typeof __typeof__
30 #endif
31 
32 #ifndef asm
33 #define asm __asm__
34 #endif
35 
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
41 
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
46 
51 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0) && HAS_XSK_UMEM_SHARED == 1
52 #define CAN_USE_XSK_UMEM_SHARED 1
53 #endif
54 
67 #define CNE_DEFAULT_SET(x, y, z) \
68  do { \
69  if (x == y) \
70  x = z; \
71  } while ((0))
72 
83 #define CNE_MAX_SET(x, z) \
84  do { \
85  if (x > z) \
86  x = z; \
87  } while ((0))
88 
90 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
91 #define CNE_STD_C11 __extension__
92 #else
93 #define CNE_STD_C11
94 #endif
95 
96 typedef uint64_t unaligned_uint64_t;
97 typedef uint32_t unaligned_uint32_t;
98 typedef uint16_t unaligned_uint16_t;
99 
100 #define CNDP_API __attribute__((visibility("default")))
101 
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)
105 #else
106 #define FUNCTION_VERSION(internal, api, ver)
107 #define DEFAULT_VERSION(internal, api, ver)
108 #endif
109 
116 #define cne_compiler_barrier() \
117  do { \
118  asm volatile("" : : : "memory"); \
119  } while (0)
120 
124 #define __cne_aligned(a) __attribute__((aligned(a)))
125 
129 #define __cne_packed __attribute__((__packed__))
130 
131 /******* Macro to mark functions and fields scheduled for removal *****/
132 #define __cne_deprecated __attribute__((__deprecated__))
133 
137 #define __cne_weak __attribute__((__weak__))
138 
139 /*********** Macros to eliminate unused variable warnings ********/
140 
144 #define __cne_unused __attribute__((__unused__))
145 
150 #define CNE_SET_USED(x) (void)(x)
151 
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)
156 
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
164 
165 #define CNE_PRIO(prio) CNE_PRIORITY_##prio
166 
176 #ifndef CNE_INIT_PRIO /* Allow to override from CNE */
177 #define CNE_INIT_PRIO(func, prio) \
178  static void __attribute__((constructor(CNE_PRIO(prio)), used)) func(void)
179 #endif
180 
189 #define CNE_INIT(func) CNE_INIT_PRIO(func, LAST)
190 
200 #ifndef CNE_FINI_PRIO /* Allow to override from CNE */
201 #define CNE_FINI_PRIO(func, prio) \
202  static void __attribute__((destructor(CNE_PRIO(prio)), used)) func(void)
203 #endif
204 
213 #define CNE_FINI(func) CNE_FINI_PRIO(func, LAST)
214 
218 #define __cne_always_inline inline __attribute__((__always_inline__))
219 
223 #define __cne_noinline __attribute__((__noinline__))
224 
228 #define CNE_PTR_CAST(ptr, x) (x)((uintptr_t)(ptr))
229 
230 /*********** Macros for pointer arithmetic ********/
231 
235 #define CNE_PTR_ADD(ptr, x) ((void *)((uintptr_t)(ptr) + (x)))
236 
240 #define CNE_PTR_SUB(ptr, x) ((void *)((uintptr_t)ptr - (x)))
241 
247 #define CNE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
248 
252 #define CNE_CAST_FIELD(var, field, type) \
253  (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
254 
255 /*********** Macros/static functions for doing alignment ********/
256 
263 #define CNE_PTR_ALIGN_FLOOR(ptr, align) ((typeof(ptr))CNE_ALIGN_FLOOR((uintptr_t)ptr, align))
264 
271 #define CNE_ALIGN_FLOOR(val, align) (typeof(val))((val) & (~((typeof(val))((align)-1))))
272 
279 #define CNE_PTR_ALIGN_CEIL(ptr, align) \
280  CNE_PTR_ALIGN_FLOOR((typeof(ptr))CNE_PTR_ADD(ptr, (align)-1), align)
281 
288 #define CNE_ALIGN_CEIL(val, align) CNE_ALIGN_FLOOR(((val) + ((typeof(val))(align)-1)), align)
289 
297 #define CNE_PTR_ALIGN(ptr, align) CNE_PTR_ALIGN_CEIL(ptr, align)
298 
306 #define CNE_ALIGN(val, align) CNE_ALIGN_CEIL(val, align)
307 
313 #define CNE_ALIGN_MUL_CEIL(v, mul) \
314  (((v + (typeof(v))(mul)-1) / ((typeof(v))(mul))) * (typeof(v))(mul))
315 
321 #define CNE_ALIGN_MUL_FLOOR(v, mul) ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
322 
328 #define CNE_ALIGN_MUL_NEAR(v, mul) \
329  ({ \
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; \
333  })
334 
346 static inline int
347 cne_is_aligned(void *ptr, unsigned align)
348 {
349  return CNE_PTR_ALIGN(ptr, (uintptr_t)align) == ptr;
350 }
351 
352 /*********** Macros for compile type checks ********/
353 
357 #define CNE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
358 
359 /*********** Cache line related macros ********/
360 
362 #define CNE_CACHE_LINE_MASK (CNE_CACHE_LINE_SIZE - 1)
363 
365 #define CNE_CACHE_LINE_ROUNDUP(size) \
366  (CNE_CACHE_LINE_SIZE * ((size + CNE_CACHE_LINE_SIZE - 1) / CNE_CACHE_LINE_SIZE))
367 
369 #if CNE_CACHE_LINE_SIZE == 64
370 #define CNE_CACHE_LINE_SIZE_LOG2 6
371 #else
372 #error "Unsupported cache line size"
373 #endif
374 
376 #define CNE_CACHE_LINE_MIN_SIZE 64
377 
379 #define __cne_cache_aligned __cne_aligned(CNE_CACHE_LINE_SIZE)
380 
382 #define __cne_cache_min_aligned __cne_aligned(CNE_CACHE_LINE_MIN_SIZE)
383 
385 __extension__ typedef void *CNE_MARKER[0];
387 __extension__ typedef uint8_t CNE_MARKER8[0];
389 __extension__ typedef uint16_t CNE_MARKER16[0];
391 __extension__ typedef uint32_t CNE_MARKER32[0];
393 __extension__ typedef uint64_t CNE_MARKER64[0];
394 
395 /*********** PA/IOVA type definitions ********/
396 
398 typedef uint64_t phys_addr_t;
399 #define CNE_BAD_PHYS_ADDR ((phys_addr_t)-1)
400 
408 typedef uint64_t cne_iova_t;
409 #define CNE_BAD_IOVA ((cne_iova_t)-1)
410 
421 static inline uint32_t
422 cne_combine32ms1b(uint32_t x)
423 {
424  x |= x >> 1;
425  x |= x >> 2;
426  x |= x >> 4;
427  x |= x >> 8;
428  x |= x >> 16;
429 
430  return x;
431 }
432 
443 static inline uint64_t
444 cne_combine64ms1b(uint64_t v)
445 {
446  v |= v >> 1;
447  v |= v >> 2;
448  v |= v >> 4;
449  v |= v >> 8;
450  v |= v >> 16;
451  v |= v >> 32;
452 
453  return v;
454 }
455 
456 /*********** Macros to work with powers of 2 ********/
457 
461 #define CNE_IS_POWER_OF_2(n) ((n) && !(((n)-1) & (n)))
462 
469 static inline int
470 cne_is_power_of_2(uint32_t n)
471 {
472  return n && !(n & (n - 1));
473 }
474 
484 static inline uint32_t
485 cne_align32pow2(uint32_t x)
486 {
487  x--;
488  x = cne_combine32ms1b(x);
489 
490  return x + 1;
491 }
492 
502 static inline uint32_t
504 {
505  x = cne_combine32ms1b(x);
506 
507  return x - (x >> 1);
508 }
509 
519 static inline uint64_t
520 cne_align64pow2(uint64_t v)
521 {
522  v--;
523  v = cne_combine64ms1b(v);
524 
525  return v + 1;
526 }
527 
537 static inline uint64_t
539 {
540  v = cne_combine64ms1b(v);
541 
542  return v - (v >> 1);
543 }
544 
545 /*********** Macros for calculating min and max **********/
546 
550 #define CNE_MIN(a, b) \
551  __extension__({ \
552  typeof(a) _a = (a); \
553  typeof(b) _b = (b); \
554  _a < _b ? _a : _b; \
555  })
556 
560 #define CNE_MAX(a, b) \
561  __extension__({ \
562  typeof(a) _a = (a); \
563  typeof(b) _b = (b); \
564  _a > _b ? _a : _b; \
565  })
566 
567 /*********** Other general functions / macros ********/
568 
580 static inline uint32_t
581 cne_bsf32(uint32_t v)
582 {
583  return (uint32_t)__builtin_ctz(v);
584 }
585 
600 static inline int
601 cne_bsf32_safe(uint64_t v, uint32_t *pos)
602 {
603  if (v == 0)
604  return 0;
605 
606  *pos = cne_bsf32(v);
607  return 1;
608 }
609 
618 static inline uint32_t
619 cne_log2_u32(uint32_t v)
620 {
621  if (v == 0)
622  return 0;
623  v = cne_align32pow2(v);
624  return cne_bsf32(v);
625 }
626 
638 static inline int
639 cne_fls_u32(uint32_t x)
640 {
641  return (x == 0) ? 0 : 32 - __builtin_clz(x);
642 }
643 
655 static inline int
656 cne_bsf64(uint64_t v)
657 {
658  return (uint32_t)__builtin_ctzll(v);
659 }
660 
675 static inline int
676 cne_bsf64_safe(uint64_t v, uint32_t *pos)
677 {
678  if (v == 0)
679  return 0;
680 
681  *pos = cne_bsf64(v);
682  return 1;
683 }
684 
697 static inline int
698 cne_fls_u64(uint64_t x)
699 {
700  return (x == 0) ? 0 : 64 - __builtin_clzll(x);
701 }
702 
711 static inline uint32_t
712 cne_log2_u64(uint64_t v)
713 {
714  if (v == 0)
715  return 0;
716  v = cne_align64pow2(v);
717  /* we checked for v being 0 already, so no undefined behavior */
718  return cne_bsf64(v);
719 }
720 
721 #ifndef offsetof
723 #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
724 #endif
725 
740 #ifndef container_of
741 #define container_of(ptr, type, member) \
742  __extension__({ \
743  const typeof(((type *)0)->member) *_ptr = (ptr); \
744  __attribute__((unused)) type *_target_ptr = (type *)(ptr); \
745  (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
746  })
747 #endif
748 
759 #define CNE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
760 
761 #define _CNE_STR(x) #x
763 #define CNE_STR(x) _CNE_STR(x)
764 
770 #define CNE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
771 #define CNE_FMT_HEAD(fmt, ...) fmt
772 #define CNE_FMT_TAIL(fmt, ...) __VA_ARGS__
773 
775 #define CNE_LEN2MASK(ln, tp) ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
776 
778 #define CNE_DIM(a) (int)(sizeof(a) / sizeof((a)[0]))
779 #define cne_countof(a) CNE_DIM(a)
780 
795 static inline uint64_t
796 cne_str_to_size(const char *str)
797 {
798  char *endptr;
799  unsigned long long size;
800 
801  while (isspace((int)*str))
802  str++;
803  if (*str == '-')
804  return 0;
805 
806  errno = 0;
807  size = strtoull(str, &endptr, 0);
808  if (errno)
809  return 0;
810 
811  if (*endptr == ' ')
812  endptr++; /* allow 1 space gap */
813 
814  switch (*endptr) {
815  case 'G':
816  case 'g':
817  size *= 1024; /* fall-through */
818  case 'M':
819  case 'm':
820  size *= 1024; /* fall-through */
821  case 'K':
822  case 'k':
823  size *= 1024; /* fall-through */
824  default:
825  break;
826  }
827  return size;
828 }
829 
830 #ifdef __cplusplus
831 }
832 #endif
833 
834 #endif
__extension__ typedef uint32_t CNE_MARKER32[0]
Definition: cne_common.h:391
static uint64_t cne_combine64ms1b(uint64_t v)
Definition: cne_common.h:444
static uint64_t cne_align64prevpow2(uint64_t v)
Definition: cne_common.h:538
static int cne_bsf32_safe(uint64_t v, uint32_t *pos)
Definition: cne_common.h:601
static uint32_t cne_log2_u32(uint32_t v)
Definition: cne_common.h:619
__extension__ typedef uint8_t CNE_MARKER8[0]
Definition: cne_common.h:387
static uint32_t cne_align32pow2(uint32_t x)
Definition: cne_common.h:485
static uint64_t cne_align64pow2(uint64_t v)
Definition: cne_common.h:520
static uint32_t cne_combine32ms1b(uint32_t x)
Definition: cne_common.h:422
static int cne_is_aligned(void *ptr, unsigned align)
Definition: cne_common.h:347
static int cne_fls_u32(uint32_t x)
Definition: cne_common.h:639
static int cne_is_power_of_2(uint32_t n)
Definition: cne_common.h:470
static uint32_t cne_log2_u64(uint64_t v)
Definition: cne_common.h:712
__extension__ typedef uint16_t CNE_MARKER16[0]
Definition: cne_common.h:389
static int cne_fls_u64(uint64_t x)
Definition: cne_common.h:698
static uint64_t cne_str_to_size(const char *str)
Definition: cne_common.h:796
__extension__ typedef void * CNE_MARKER[0]
Definition: cne_common.h:385
static int cne_bsf64(uint64_t v)
Definition: cne_common.h:656
static uint32_t cne_align32prevpow2(uint32_t x)
Definition: cne_common.h:503
__extension__ typedef uint64_t CNE_MARKER64[0]
Definition: cne_common.h:393
static uint32_t cne_bsf32(uint32_t v)
Definition: cne_common.h:581
#define CNE_PTR_ALIGN(ptr, align)
Definition: cne_common.h:297
uint64_t cne_iova_t
Definition: cne_common.h:408
uint64_t phys_addr_t
Definition: cne_common.h:398
static int cne_bsf64_safe(uint64_t v, uint32_t *pos)
Definition: cne_common.h:676