mbuf: support dynamic fields and flags
[dpdk.git] / lib / librte_eal / common / include / rte_common.h
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 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24
25 #include <rte_config.h>
26
27 /* OS specific include */
28 #include <rte_os.h>
29
30 #ifndef typeof
31 #define typeof __typeof__
32 #endif
33
34 #ifndef asm
35 #define asm __asm__
36 #endif
37
38 /** C extension macro for environments lacking C11 features. */
39 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
40 #define RTE_STD_C11 __extension__
41 #else
42 #define RTE_STD_C11
43 #endif
44
45 /** Define GCC_VERSION **/
46 #ifdef RTE_TOOLCHAIN_GCC
47 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 +  \
48                 __GNUC_PATCHLEVEL__)
49 #endif
50
51 #ifdef RTE_ARCH_STRICT_ALIGN
52 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
53 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
54 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
55 #else
56 typedef uint64_t unaligned_uint64_t;
57 typedef uint32_t unaligned_uint32_t;
58 typedef uint16_t unaligned_uint16_t;
59 #endif
60
61 /**
62  * Force alignment
63  */
64 #define __rte_aligned(a) __attribute__((__aligned__(a)))
65
66 /**
67  * Force a structure to be packed
68  */
69 #define __rte_packed __attribute__((__packed__))
70
71 /******* Macro to mark functions and fields scheduled for removal *****/
72 #define __rte_deprecated        __attribute__((__deprecated__))
73
74 /**
75  * Mark a function or variable to a weak reference.
76  */
77 #define __rte_weak __attribute__((__weak__))
78
79 /*********** Macros to eliminate unused variable warnings ********/
80
81 /**
82  * short definition to mark a function parameter unused
83  */
84 #define __rte_unused __attribute__((__unused__))
85
86 /**
87  * definition to mark a variable or function parameter as used so
88  * as to avoid a compiler warning
89  */
90 #define RTE_SET_USED(x) (void)(x)
91
92 #define RTE_PRIORITY_LOG 101
93 #define RTE_PRIORITY_BUS 110
94 #define RTE_PRIORITY_CLASS 120
95 #define RTE_PRIORITY_LAST 65535
96
97 #define RTE_PRIO(prio) \
98         RTE_PRIORITY_ ## prio
99
100 /**
101  * Run function before main() with high priority.
102  *
103  * @param func
104  *   Constructor function.
105  * @param prio
106  *   Priority number must be above 100.
107  *   Lowest number is the first to run.
108  */
109 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
110 #define RTE_INIT_PRIO(func, prio) \
111 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
112 #endif
113
114 /**
115  * Run function before main() with low priority.
116  *
117  * The constructor will be run after prioritized constructors.
118  *
119  * @param func
120  *   Constructor function.
121  */
122 #define RTE_INIT(func) \
123         RTE_INIT_PRIO(func, LAST)
124
125 /**
126  * Run after main() with low priority.
127  *
128  * @param func
129  *   Destructor function name.
130  * @param prio
131  *   Priority number must be above 100.
132  *   Lowest number is the last to run.
133  */
134 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
135 #define RTE_FINI_PRIO(func, prio) \
136 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
137 #endif
138
139 /**
140  * Run after main() with high priority.
141  *
142  * The destructor will be run *before* prioritized destructors.
143  *
144  * @param func
145  *   Destructor function name.
146  */
147 #define RTE_FINI(func) \
148         RTE_FINI_PRIO(func, LAST)
149
150 /**
151  * Force a function to be inlined
152  */
153 #define __rte_always_inline inline __attribute__((always_inline))
154
155 /**
156  * Force a function to be noinlined
157  */
158 #define __rte_noinline  __attribute__((noinline))
159
160 /*********** Macros for pointer arithmetic ********/
161
162 /**
163  * add a byte-value offset to a pointer
164  */
165 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
166
167 /**
168  * subtract a byte-value offset from a pointer
169  */
170 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
171
172 /**
173  * get the difference between two pointer values, i.e. how far apart
174  * in bytes are the locations they point two. It is assumed that
175  * ptr1 is greater than ptr2.
176  */
177 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
178
179 /**
180  * Workaround to cast a const field of a structure to non-const type.
181  */
182 #define RTE_CAST_FIELD(var, field, type) \
183         (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
184
185 /*********** Macros/static functions for doing alignment ********/
186
187
188 /**
189  * Macro to align a pointer to a given power-of-two. The resultant
190  * pointer will be a pointer of the same type as the first parameter, and
191  * point to an address no higher than the first parameter. Second parameter
192  * must be a power-of-two value.
193  */
194 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
195         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
196
197 /**
198  * Macro to align a value to a given power-of-two. The resultant value
199  * will be of the same type as the first parameter, and will be no
200  * bigger than the first parameter. Second parameter must be a
201  * power-of-two value.
202  */
203 #define RTE_ALIGN_FLOOR(val, align) \
204         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
205
206 /**
207  * Macro to align a pointer to a given power-of-two. The resultant
208  * pointer will be a pointer of the same type as the first parameter, and
209  * point to an address no lower than the first parameter. Second parameter
210  * must be a power-of-two value.
211  */
212 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
213         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
214
215 /**
216  * Macro to align a value to a given power-of-two. The resultant value
217  * will be of the same type as the first parameter, and will be no lower
218  * than the first parameter. Second parameter must be a power-of-two
219  * value.
220  */
221 #define RTE_ALIGN_CEIL(val, align) \
222         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
223
224 /**
225  * Macro to align a pointer to a given power-of-two. The resultant
226  * pointer will be a pointer of the same type as the first parameter, and
227  * point to an address no lower than the first parameter. Second parameter
228  * must be a power-of-two value.
229  * This function is the same as RTE_PTR_ALIGN_CEIL
230  */
231 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
232
233 /**
234  * Macro to align a value to a given power-of-two. The resultant
235  * value will be of the same type as the first parameter, and
236  * will be no lower than the first parameter. Second parameter
237  * must be a power-of-two value.
238  * This function is the same as RTE_ALIGN_CEIL
239  */
240 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
241
242 /**
243  * Macro to align a value to the multiple of given value. The resultant
244  * value will be of the same type as the first parameter and will be no lower
245  * than the first parameter.
246  */
247 #define RTE_ALIGN_MUL_CEIL(v, mul) \
248         (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
249
250 /**
251  * Macro to align a value to the multiple of given value. The resultant
252  * value will be of the same type as the first parameter and will be no higher
253  * than the first parameter.
254  */
255 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
256         ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
257
258 /**
259  * Macro to align value to the nearest multiple of the given value.
260  * The resultant value might be greater than or less than the first parameter
261  * whichever difference is the lowest.
262  */
263 #define RTE_ALIGN_MUL_NEAR(v, mul)                              \
264         ({                                                      \
265                 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul);    \
266                 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul);  \
267                 (ceil - v) > (v - floor) ? floor : ceil;        \
268         })
269
270 /**
271  * Checks if a pointer is aligned to a given power-of-two value
272  *
273  * @param ptr
274  *   The pointer whose alignment is to be checked
275  * @param align
276  *   The power-of-two value to which the ptr should be aligned
277  *
278  * @return
279  *   True(1) where the pointer is correctly aligned, false(0) otherwise
280  */
281 static inline int
282 rte_is_aligned(void *ptr, unsigned align)
283 {
284         return RTE_PTR_ALIGN(ptr, align) == ptr;
285 }
286
287 /*********** Macros for compile type checks ********/
288
289 /**
290  * Triggers an error at compilation time if the condition is true.
291  */
292 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
293
294 /*********** Cache line related macros ********/
295
296 /** Cache line mask. */
297 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1)
298
299 /** Return the first cache-aligned value greater or equal to size. */
300 #define RTE_CACHE_LINE_ROUNDUP(size) \
301         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / \
302         RTE_CACHE_LINE_SIZE))
303
304 /** Cache line size in terms of log2 */
305 #if RTE_CACHE_LINE_SIZE == 64
306 #define RTE_CACHE_LINE_SIZE_LOG2 6
307 #elif RTE_CACHE_LINE_SIZE == 128
308 #define RTE_CACHE_LINE_SIZE_LOG2 7
309 #else
310 #error "Unsupported cache line size"
311 #endif
312
313 /** Minimum Cache line size. */
314 #define RTE_CACHE_LINE_MIN_SIZE 64
315
316 /** Force alignment to cache line. */
317 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
318
319 /** Force minimum cache line alignment. */
320 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
321
322 /*********** PA/IOVA type definitions ********/
323
324 /** Physical address */
325 typedef uint64_t phys_addr_t;
326 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
327
328 /**
329  * IO virtual address type.
330  * When the physical addressing mode (IOVA as PA) is in use,
331  * the translation from an IO virtual address (IOVA) to a physical address
332  * is a direct mapping, i.e. the same value.
333  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
334  */
335 typedef uint64_t rte_iova_t;
336 #define RTE_BAD_IOVA ((rte_iova_t)-1)
337
338
339 /**
340  * Combines 32b inputs most significant set bits into the least
341  * significant bits to construct a value with the same MSBs as x
342  * but all 1's under it.
343  *
344  * @param x
345  *    The integer whose MSBs need to be combined with its LSBs
346  * @return
347  *    The combined value.
348  */
349 static inline uint32_t
350 rte_combine32ms1b(register uint32_t x)
351 {
352         x |= x >> 1;
353         x |= x >> 2;
354         x |= x >> 4;
355         x |= x >> 8;
356         x |= x >> 16;
357
358         return x;
359 }
360
361 /**
362  * Combines 64b inputs most significant set bits into the least
363  * significant bits to construct a value with the same MSBs as x
364  * but all 1's under it.
365  *
366  * @param v
367  *    The integer whose MSBs need to be combined with its LSBs
368  * @return
369  *    The combined value.
370  */
371 static inline uint64_t
372 rte_combine64ms1b(register uint64_t v)
373 {
374         v |= v >> 1;
375         v |= v >> 2;
376         v |= v >> 4;
377         v |= v >> 8;
378         v |= v >> 16;
379         v |= v >> 32;
380
381         return v;
382 }
383
384 /*********** Macros to work with powers of 2 ********/
385
386 /**
387  * Macro to return 1 if n is a power of 2, 0 otherwise
388  */
389 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
390
391 /**
392  * Returns true if n is a power of 2
393  * @param n
394  *     Number to check
395  * @return 1 if true, 0 otherwise
396  */
397 static inline int
398 rte_is_power_of_2(uint32_t n)
399 {
400         return n && !(n & (n - 1));
401 }
402
403 /**
404  * Aligns input parameter to the next power of 2
405  *
406  * @param x
407  *   The integer value to align
408  *
409  * @return
410  *   Input parameter aligned to the next power of 2
411  */
412 static inline uint32_t
413 rte_align32pow2(uint32_t x)
414 {
415         x--;
416         x = rte_combine32ms1b(x);
417
418         return x + 1;
419 }
420
421 /**
422  * Aligns input parameter to the previous power of 2
423  *
424  * @param x
425  *   The integer value to align
426  *
427  * @return
428  *   Input parameter aligned to the previous power of 2
429  */
430 static inline uint32_t
431 rte_align32prevpow2(uint32_t x)
432 {
433         x = rte_combine32ms1b(x);
434
435         return x - (x >> 1);
436 }
437
438 /**
439  * Aligns 64b input parameter to the next power of 2
440  *
441  * @param v
442  *   The 64b value to align
443  *
444  * @return
445  *   Input parameter aligned to the next power of 2
446  */
447 static inline uint64_t
448 rte_align64pow2(uint64_t v)
449 {
450         v--;
451         v = rte_combine64ms1b(v);
452
453         return v + 1;
454 }
455
456 /**
457  * Aligns 64b input parameter to the previous power of 2
458  *
459  * @param v
460  *   The 64b value to align
461  *
462  * @return
463  *   Input parameter aligned to the previous power of 2
464  */
465 static inline uint64_t
466 rte_align64prevpow2(uint64_t v)
467 {
468         v = rte_combine64ms1b(v);
469
470         return v - (v >> 1);
471 }
472
473 /*********** Macros for calculating min and max **********/
474
475 /**
476  * Macro to return the minimum of two numbers
477  */
478 #define RTE_MIN(a, b) \
479         __extension__ ({ \
480                 typeof (a) _a = (a); \
481                 typeof (b) _b = (b); \
482                 _a < _b ? _a : _b; \
483         })
484
485 /**
486  * Macro to return the maximum of two numbers
487  */
488 #define RTE_MAX(a, b) \
489         __extension__ ({ \
490                 typeof (a) _a = (a); \
491                 typeof (b) _b = (b); \
492                 _a > _b ? _a : _b; \
493         })
494
495 /*********** Other general functions / macros ********/
496
497 /**
498  * Searches the input parameter for the least significant set bit
499  * (starting from zero).
500  * If a least significant 1 bit is found, its bit index is returned.
501  * If the content of the input parameter is zero, then the content of the return
502  * value is undefined.
503  * @param v
504  *     input parameter, should not be zero.
505  * @return
506  *     least significant set bit in the input parameter.
507  */
508 static inline uint32_t
509 rte_bsf32(uint32_t v)
510 {
511         return (uint32_t)__builtin_ctz(v);
512 }
513
514 /**
515  * Searches the input parameter for the least significant set bit
516  * (starting from zero). Safe version (checks for input parameter being zero).
517  *
518  * @warning ``pos`` must be a valid pointer. It is not checked!
519  *
520  * @param v
521  *     The input parameter.
522  * @param pos
523  *     If ``v`` was not 0, this value will contain position of least significant
524  *     bit within the input parameter.
525  * @return
526  *     Returns 0 if ``v`` was 0, otherwise returns 1.
527  */
528 static inline int
529 rte_bsf32_safe(uint64_t v, uint32_t *pos)
530 {
531         if (v == 0)
532                 return 0;
533
534         *pos = rte_bsf32(v);
535         return 1;
536 }
537
538 /**
539  * Return the rounded-up log2 of a integer.
540  *
541  * @param v
542  *     The input parameter.
543  * @return
544  *     The rounded-up log2 of the input, or 0 if the input is 0.
545  */
546 static inline uint32_t
547 rte_log2_u32(uint32_t v)
548 {
549         if (v == 0)
550                 return 0;
551         v = rte_align32pow2(v);
552         return rte_bsf32(v);
553 }
554
555
556 /**
557  * Return the last (most-significant) bit set.
558  *
559  * @note The last (most significant) bit is at position 32.
560  * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
561  *
562  * @param x
563  *     The input parameter.
564  * @return
565  *     The last (most-significant) bit set, or 0 if the input is 0.
566  */
567 static inline int
568 rte_fls_u32(uint32_t x)
569 {
570         return (x == 0) ? 0 : 32 - __builtin_clz(x);
571 }
572
573 /**
574  * Searches the input parameter for the least significant set bit
575  * (starting from zero).
576  * If a least significant 1 bit is found, its bit index is returned.
577  * If the content of the input parameter is zero, then the content of the return
578  * value is undefined.
579  * @param v
580  *     input parameter, should not be zero.
581  * @return
582  *     least significant set bit in the input parameter.
583  */
584 static inline int
585 rte_bsf64(uint64_t v)
586 {
587         return (uint32_t)__builtin_ctzll(v);
588 }
589
590 /**
591  * Searches the input parameter for the least significant set bit
592  * (starting from zero). Safe version (checks for input parameter being zero).
593  *
594  * @warning ``pos`` must be a valid pointer. It is not checked!
595  *
596  * @param v
597  *     The input parameter.
598  * @param pos
599  *     If ``v`` was not 0, this value will contain position of least significant
600  *     bit within the input parameter.
601  * @return
602  *     Returns 0 if ``v`` was 0, otherwise returns 1.
603  */
604 static inline int
605 rte_bsf64_safe(uint64_t v, uint32_t *pos)
606 {
607         if (v == 0)
608                 return 0;
609
610         *pos = rte_bsf64(v);
611         return 1;
612 }
613
614 /**
615  * Return the last (most-significant) bit set.
616  *
617  * @note The last (most significant) bit is at position 64.
618  * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
619  *       rte_fls_u64(0x8000000000000000) = 64
620  *
621  * @param x
622  *     The input parameter.
623  * @return
624  *     The last (most-significant) bit set, or 0 if the input is 0.
625  */
626 static inline int
627 rte_fls_u64(uint64_t x)
628 {
629         return (x == 0) ? 0 : 64 - __builtin_clzll(x);
630 }
631
632 /**
633  * Return the rounded-up log2 of a 64-bit integer.
634  *
635  * @param v
636  *     The input parameter.
637  * @return
638  *     The rounded-up log2 of the input, or 0 if the input is 0.
639  */
640 static inline uint32_t
641 rte_log2_u64(uint64_t v)
642 {
643         if (v == 0)
644                 return 0;
645         v = rte_align64pow2(v);
646         /* we checked for v being 0 already, so no undefined behavior */
647         return rte_bsf64(v);
648 }
649
650 #ifndef offsetof
651 /** Return the offset of a field in a structure. */
652 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
653 #endif
654
655 /**
656  * Return pointer to the wrapping struct instance.
657  *
658  * Example:
659  *
660  *  struct wrapper {
661  *      ...
662  *      struct child c;
663  *      ...
664  *  };
665  *
666  *  struct child *x = obtain(...);
667  *  struct wrapper *w = container_of(x, struct wrapper, c);
668  */
669 #ifndef container_of
670 #define container_of(ptr, type, member) __extension__ ({                \
671                         const typeof(((type *)0)->member) *_ptr = (ptr); \
672                         __attribute__((unused)) type *_target_ptr =     \
673                                 (type *)(ptr);                          \
674                         (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
675                 })
676 #endif
677
678 /**
679  * Get the size of a field in a structure.
680  *
681  * @param type
682  *   The type of the structure.
683  * @param field
684  *   The field in the structure.
685  * @return
686  *   The size of the field in the structure, in bytes.
687  */
688 #define RTE_SIZEOF_FIELD(type, field) (sizeof(((type *)0)->field))
689
690 #define _RTE_STR(x) #x
691 /** Take a macro value and get a string version of it */
692 #define RTE_STR(x) _RTE_STR(x)
693
694 /**
695  * ISO C helpers to modify format strings using variadic macros.
696  * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
697  * An empty %s argument is appended to avoid a dangling comma.
698  */
699 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
700 #define RTE_FMT_HEAD(fmt, ...) fmt
701 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
702
703 /** Mask value of type "tp" for the first "ln" bit set. */
704 #define RTE_LEN2MASK(ln, tp)    \
705         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
706
707 /** Number of elements in the array. */
708 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
709
710 /**
711  * Converts a numeric string to the equivalent uint64_t value.
712  * As well as straight number conversion, also recognises the suffixes
713  * k, m and g for kilobytes, megabytes and gigabytes respectively.
714  *
715  * If a negative number is passed in  i.e. a string with the first non-black
716  * character being "-", zero is returned. Zero is also returned in the case of
717  * an error with the strtoull call in the function.
718  *
719  * @param str
720  *     String containing number to convert.
721  * @return
722  *     Number.
723  */
724 static inline uint64_t
725 rte_str_to_size(const char *str)
726 {
727         char *endptr;
728         unsigned long long size;
729
730         while (isspace((int)*str))
731                 str++;
732         if (*str == '-')
733                 return 0;
734
735         errno = 0;
736         size = strtoull(str, &endptr, 0);
737         if (errno)
738                 return 0;
739
740         if (*endptr == ' ')
741                 endptr++; /* allow 1 space gap */
742
743         switch (*endptr){
744         case 'G': case 'g': size *= 1024; /* fall-through */
745         case 'M': case 'm': size *= 1024; /* fall-through */
746         case 'K': case 'k': size *= 1024; /* fall-through */
747         default:
748                 break;
749         }
750         return size;
751 }
752
753 /**
754  * Function to terminate the application immediately, printing an error
755  * message and returning the exit_code back to the shell.
756  *
757  * This function never returns
758  *
759  * @param exit_code
760  *     The exit code to be returned by the application
761  * @param format
762  *     The format string to be used for printing the message. This can include
763  *     printf format characters which will be expanded using any further parameters
764  *     to the function.
765  */
766 void
767 rte_exit(int exit_code, const char *format, ...)
768         __attribute__((noreturn))
769         __attribute__((format(printf, 2, 3)));
770
771 #ifdef __cplusplus
772 }
773 #endif
774
775 #endif