1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2015 Mellanox Technologies, Ltd
6 #ifndef RTE_PMD_MLX5_UTILS_H_
7 #define RTE_PMD_MLX5_UTILS_H_
15 #include <rte_spinlock.h>
16 #include <rte_rwlock.h>
17 #include <rte_memory.h>
18 #include <rte_bitmap.h>
20 #include <mlx5_common.h>
22 #include "mlx5_defs.h"
24 #define mlx5_hlist_remove(h, e) \
25 mlx5_hlist_unregister(h, e)
27 #define mlx5_hlist_insert(h, e) \
28 mlx5_hlist_register(h, 0, e)
30 /* Convert a bit number to the corresponding 64-bit mask */
31 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
33 /* Save and restore errno around argument evaluation. */
34 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
36 extern int mlx5_logtype;
38 /* Generic printf()-like logging macro with automatic line feed. */
39 #define DRV_LOG(level, ...) \
40 PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \
41 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
44 /* Convenience macros for accessing mbuf fields. */
45 #define NEXT(m) ((m)->next)
46 #define DATA_LEN(m) ((m)->data_len)
47 #define PKT_LEN(m) ((m)->pkt_len)
48 #define DATA_OFF(m) ((m)->data_off)
49 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
50 #define NB_SEGS(m) ((m)->nb_segs)
51 #define PORT(m) ((m)->port)
53 /* Transpose flags. Useful to convert IBV to DPDK flags. */
54 #define TRANSPOSE(val, from, to) \
56 (((val) & (from)) / ((from) / (to))) : \
57 (((val) & (from)) * ((to) / (from))))
60 * For the case which data is linked with sequence increased index, the
61 * array table will be more efficiect than hash table once need to serarch
62 * one data entry in large numbers of entries. Since the traditional hash
63 * tables has fixed table size, when huge numbers of data saved to the hash
64 * table, it also comes lots of hash conflict.
66 * But simple array table also has fixed size, allocates all the needed
67 * memory at once will waste lots of memory. For the case don't know the
68 * exactly number of entries will be impossible to allocate the array.
70 * Then the multiple level table helps to balance the two disadvantages.
71 * Allocate a global high level table with sub table entries at first,
72 * the global table contains the sub table entries, and the sub table will
73 * be allocated only once the corresponding index entry need to be saved.
74 * e.g. for up to 32-bits index, three level table with 10-10-12 splitting,
75 * with sequence increased index, the memory grows with every 4K entries.
77 * The currently implementation introduces 10-10-12 32-bits splitting
78 * Three-Level table to help the cases which have millions of enties to
79 * save. The index entries can be addressed directly by the index, no
80 * search will be needed.q
83 /* L3 table global table define. */
84 #define MLX5_L3T_GT_OFFSET 22
85 #define MLX5_L3T_GT_SIZE (1 << 10)
86 #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1)
88 /* L3 table middle table define. */
89 #define MLX5_L3T_MT_OFFSET 12
90 #define MLX5_L3T_MT_SIZE (1 << 10)
91 #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1)
93 /* L3 table entry table define. */
94 #define MLX5_L3T_ET_OFFSET 0
95 #define MLX5_L3T_ET_SIZE (1 << 12)
96 #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1)
100 MLX5_L3T_TYPE_WORD = 0,
107 struct mlx5_indexed_pool;
109 /* Generic data struct. */
110 union mlx5_l3t_data {
117 /* L3 level table data structure. */
118 struct mlx5_l3t_level_tbl {
119 uint64_t ref_cnt; /* Table ref_cnt. */
120 void *tbl[]; /* Table array. */
123 /* L3 word entry table data structure. */
124 struct mlx5_l3t_entry_word {
125 uint32_t idx; /* Table index. */
126 uint64_t ref_cnt; /* Table ref_cnt. */
130 } entry[MLX5_L3T_ET_SIZE]; /* Entry array */
133 /* L3 double word entry table data structure. */
134 struct mlx5_l3t_entry_dword {
135 uint32_t idx; /* Table index. */
136 uint64_t ref_cnt; /* Table ref_cnt. */
140 } entry[MLX5_L3T_ET_SIZE]; /* Entry array */
143 /* L3 quad word entry table data structure. */
144 struct mlx5_l3t_entry_qword {
145 uint32_t idx; /* Table index. */
146 uint64_t ref_cnt; /* Table ref_cnt. */
150 } entry[MLX5_L3T_ET_SIZE]; /* Entry array */
153 /* L3 pointer entry table data structure. */
154 struct mlx5_l3t_entry_ptr {
155 uint32_t idx; /* Table index. */
156 uint64_t ref_cnt; /* Table ref_cnt. */
160 } entry[MLX5_L3T_ET_SIZE]; /* Entry array */
163 /* L3 table data structure. */
164 struct mlx5_l3t_tbl {
165 enum mlx5_l3t_type type; /* Table type. */
166 struct mlx5_indexed_pool *eip;
167 /* Table index pool handles. */
168 struct mlx5_l3t_level_tbl *tbl; /* Global table index. */
169 rte_spinlock_t sl; /* The table lock. */
172 /** Type of function that is used to handle the data before freeing. */
173 typedef int32_t (*mlx5_l3t_alloc_callback_fn)(void *ctx,
174 union mlx5_l3t_data *data);
177 * The indexed memory entry index is made up of trunk index and offset of
178 * the entry in the trunk. Since the entry index is 32 bits, in case user
179 * prefers to have small trunks, user can change the macro below to a big
180 * number which helps the pool contains more trunks with lots of entries
183 #define TRUNK_IDX_BITS 16
184 #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
185 #define TRUNK_INVALID TRUNK_MAX_IDX
186 #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
187 #ifdef RTE_LIBRTE_MLX5_DEBUG
191 struct mlx5_indexed_pool_config {
192 uint32_t size; /* Pool entry size. */
193 uint32_t trunk_size:22;
195 * Trunk entry number. Must be power of 2. It can be increased
196 * if trunk_grow enable. The trunk entry number increases with
197 * left shift grow_shift. Trunks with index are after grow_trunk
198 * will keep the entry number same with the last grow trunk.
200 uint32_t grow_trunk:4;
202 * Trunks with entry number increase in the pool. Set it to 0
203 * to make the pool works as trunk entry fixed pool. It works
204 * only if grow_shift is not 0.
206 uint32_t grow_shift:4;
208 * Trunk entry number increase shift value, stop after grow_trunk.
209 * It works only if grow_trunk is not 0.
211 uint32_t need_lock:1;
212 /* Lock is needed for multiple thread usage. */
213 uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
214 const char *type; /* Memory allocate type name. */
215 void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
217 /* User defined memory allocator. */
218 void (*free)(void *addr); /* User defined memory release. */
221 struct mlx5_indexed_trunk {
222 uint32_t idx; /* Trunk id. */
223 uint32_t prev; /* Previous free trunk in free list. */
224 uint32_t next; /* Next free trunk in free list. */
225 uint32_t free; /* Free entries available */
226 struct rte_bitmap *bmp;
227 uint8_t data[] __rte_cache_aligned; /* Entry data start. */
230 struct mlx5_indexed_pool {
231 struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
232 rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
233 uint32_t n_trunk_valid; /* Trunks allocated. */
234 uint32_t n_trunk; /* Trunk pointer array size. */
235 /* Dim of trunk pointer array. */
236 struct mlx5_indexed_trunk **trunks;
237 uint32_t free_list; /* Index to first free trunk. */
241 uint32_t trunk_avail;
242 uint32_t trunk_empty;
245 uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
249 * Return logarithm of the nearest power of two above input value.
255 * Logarithm of the nearest power of two above input value.
257 static inline unsigned int
258 log2above(unsigned int v)
263 for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
268 #define MLX5_HLIST_DIRECT_KEY 0x0001 /* Use the key directly as hash index. */
269 #define MLX5_HLIST_WRITE_MOST 0x0002 /* List mostly used for append new. */
271 /** Maximum size of string for naming the hlist table. */
272 #define MLX5_HLIST_NAMESIZE 32
277 * Structure of the entry in the hash list, user should define its own struct
278 * that contains this in order to store the data. The 'key' is 64-bits right
279 * now and its user's responsibility to guarantee there is no collision.
281 struct mlx5_hlist_entry {
282 LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
283 uint64_t key; /* user defined 'key', could be the hash signature. */
284 uint32_t ref_cnt; /* Reference count. */
287 /** Structure for hash head. */
288 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
290 /** Type of function that is used to handle the data before freeing. */
291 typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
294 * Type of function for user defined matching.
297 * The entry in the list.
299 * The pointer to new entry context.
302 * 0 if matching, -1 otherwise.
304 typedef int (*mlx5_hlist_match_callback_fn)(struct mlx5_hlist_entry *entry,
308 * Type of callback function for entry removal.
313 * The entry in the list.
315 typedef void (*mlx5_hlist_remove_cb)(struct mlx5_hlist *list,
316 struct mlx5_hlist_entry *entry);
319 * Type of function for user defined matching.
324 * The entry in the list.
328 * The pointer to new entry context.
331 * 0 if matching, non-zero number otherwise.
333 typedef int (*mlx5_hlist_match_cb)(struct mlx5_hlist *list,
334 struct mlx5_hlist_entry *entry,
335 uint64_t key, void *ctx);
338 * Type of function for user defined hash list entry creation.
343 * The key of the new entry.
345 * The pointer to new entry context.
348 * Pointer to allocated entry on success, NULL otherwise.
350 typedef struct mlx5_hlist_entry *(*mlx5_hlist_create_cb)
351 (struct mlx5_hlist *list,
352 uint64_t key, void *ctx);
355 * Hash list table structure
357 * Entry in hash list could be reused if entry already exists, reference
358 * count will increase and the existing entry returns.
360 * When destroy an entry from list, decrease reference count and only
361 * destroy when no further reference.
364 char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
365 /**< number of heads, need to be power of 2. */
367 uint32_t entry_sz; /**< Size of entry, used to allocate entry. */
368 /**< mask to get the index of the list heads. */
371 uint32_t gen_cnt; /* List modification will update generation count. */
372 bool direct_key; /* Use the new entry key directly as hash index. */
373 bool write_most; /* List mostly used for append new or destroy. */
375 mlx5_hlist_create_cb cb_create; /**< entry create callback. */
376 mlx5_hlist_match_cb cb_match; /**< entry match callback. */
377 mlx5_hlist_remove_cb cb_remove; /**< entry remove callback. */
378 struct mlx5_hlist_head heads[]; /**< list head arrays. */
382 * Create a hash list table, the user can specify the list heads array size
383 * of the table, now the size should be a power of 2 in order to get better
384 * distribution for the entries. Each entry is a part of the whole data element
385 * and the caller should be responsible for the data element's allocation and
386 * cleanup / free. Key of each entry will be calculated with CRC in order to
387 * generate a little fairer distribution.
390 * Name of the hash list(optional).
392 * Heads array size of the hash list.
394 * Entry size to allocate if cb_create not specified.
396 * The hash list attribute flags.
398 * Callback function for entry create.
400 * Callback function for entry match.
402 * Callback function for entry destroy.
404 * Pointer of the hash list table created, NULL on failure.
406 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size,
407 uint32_t entry_size, uint32_t flags,
408 mlx5_hlist_create_cb cb_create,
409 mlx5_hlist_match_cb cb_match,
410 mlx5_hlist_remove_cb cb_destroy);
413 * Search an entry matching the key.
415 * Result returned might be destroyed by other thread, must use
416 * this function only in main thread.
419 * Pointer to the hast list table.
421 * Key for the searching entry.
423 * Common context parameter used by entry callback function.
426 * Pointer of the hlist entry if found, NULL otherwise.
428 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key,
432 * Extended routine to search an entry matching the context with
433 * user defined match function.
436 * Pointer to the hast list table.
438 * Key for the searching entry.
440 * Callback function to match the node with context.
442 * Common context parameter used by callback function.
445 * Pointer of the hlist entry if found, NULL otherwise.
447 struct mlx5_hlist_entry *mlx5_hlist_lookup_ex(struct mlx5_hlist *h,
449 mlx5_hlist_match_callback_fn cb,
453 * Extended routine to insert an entry to the list with key collisions.
455 * For the list have key collision, the extra user defined match function
456 * allows node with same key will be inserted.
459 * Pointer to the hast list table.
461 * Entry to be inserted into the hash list table.
463 * Callback function to match the node with context.
465 * Common context parameter used by callback function.
468 * - zero for success.
469 * - -EEXIST if the entry is already inserted.
471 int mlx5_hlist_insert_ex(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry,
472 mlx5_hlist_match_callback_fn cb, void *ctx);
475 * Insert an entry to the hash list table, the entry is only part of whole data
476 * element and a 64B key is used for matching. User should construct the key or
477 * give a calculated hash signature and guarantee there is no collision.
480 * Pointer to the hast list table.
482 * Entry to be inserted into the hash list table.
484 * Common context parameter used by callback function.
487 * registered entry on success, NULL otherwise
489 struct mlx5_hlist_entry *mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key,
493 * Remove an entry from the hash list table. User should guarantee the validity
497 * Pointer to the hast list table. (not used)
499 * Entry to be removed from the hash list table.
501 * 0 on entry removed, 1 on entry still referenced.
503 int mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
506 * Destroy the hash list table, all the entries already inserted into the lists
507 * will be handled by the callback function provided by the user (including
508 * free if needed) before the table is freed.
511 * Pointer to the hast list table.
513 void mlx5_hlist_destroy(struct mlx5_hlist *h);
516 * This function allocates non-initialized memory entry from pool.
517 * In NUMA systems, the memory entry allocated resides on the same
518 * NUMA socket as the core that calls this function.
520 * Memory entry is allocated from memory trunk, no alignment.
523 * Pointer to indexed memory entry pool.
524 * No initialization required.
526 * Pointer to memory to save allocated index.
527 * Memory index always positive value.
529 * - Pointer to the allocated memory entry.
530 * - NULL on error. Not enough memory, or invalid arguments.
532 void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
535 * This function allocates zero initialized memory entry from pool.
536 * In NUMA systems, the memory entry allocated resides on the same
537 * NUMA socket as the core that calls this function.
539 * Memory entry is allocated from memory trunk, no alignment.
542 * Pointer to indexed memory pool.
543 * No initialization required.
545 * Pointer to memory to save allocated index.
546 * Memory index always positive value.
548 * - Pointer to the allocated memory entry .
549 * - NULL on error. Not enough memory, or invalid arguments.
551 void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
554 * This function frees indexed memory entry to pool.
555 * Caller has to make sure that the index is allocated from same pool.
558 * Pointer to indexed memory pool.
560 * Allocated memory entry index.
562 void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
565 * This function returns pointer of indexed memory entry from index.
566 * Caller has to make sure that the index is valid, and allocated
570 * Pointer to indexed memory pool.
572 * Allocated memory index.
574 * - Pointer to indexed memory entry.
576 void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
579 * This function creates indexed memory pool.
580 * Caller has to configure the configuration accordingly.
583 * Pointer to indexed memory pool.
585 * Allocated memory index.
587 struct mlx5_indexed_pool *
588 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
591 * This function releases all resources of pool.
592 * Caller has to make sure that all indexes and memories allocated
593 * from this pool not referenced anymore.
596 * Pointer to indexed memory pool.
598 * - non-zero value on error.
601 int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
604 * This function dumps debug info of pool.
607 * Pointer to indexed memory pool.
609 void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
612 * This function allocates new empty Three-level table.
615 * The l3t can set as word, double word, quad word or pointer with index.
618 * - Pointer to the allocated l3t.
619 * - NULL on error. Not enough memory, or invalid arguments.
621 struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
624 * This function destroys Three-level table.
627 * Pointer to the l3t.
629 void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
632 * This function gets the index entry from Three-level table.
635 * Pointer to the l3t.
637 * Index to the entry.
639 * Pointer to the memory which saves the entry data.
640 * When function call returns 0, data contains the entry data get from
642 * When function call returns -1, data is not modified.
645 * 0 if success, -1 on error.
648 int32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
649 union mlx5_l3t_data *data);
652 * This function gets the index entry from Three-level table.
654 * If the index entry is not available, allocate new one by callback
655 * function and fill in the entry.
658 * Pointer to the l3t.
660 * Index to the entry.
662 * Pointer to the memory which saves the entry data.
663 * When function call returns 0, data contains the entry data get from
665 * When function call returns -1, data is not modified.
667 * Callback function to allocate new data.
669 * Context for callback function.
672 * 0 if success, -1 on error.
675 int32_t mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
676 union mlx5_l3t_data *data,
677 mlx5_l3t_alloc_callback_fn cb, void *ctx);
680 * This function decreases and clear index entry if reference
681 * counter is 0 from Three-level table.
684 * Pointer to the l3t.
686 * Index to the entry.
689 * The remaining reference count, 0 means entry be cleared, -1 on error.
691 int32_t mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
694 * This function sets the index entry to Three-level table.
695 * If the entry is already set, the EEXIST errno will be given, and
696 * the set data will be filled to the data.
699 * Pointer to the l3t.
701 * Index to the entry.
702 * @param data[in/out]
703 * Pointer to the memory which contains the entry data save to l3t.
704 * If the entry is already set, the set data will be filled.
707 * 0 if success, -1 on error.
709 int32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
710 union mlx5_l3t_data *data);
713 * Macros for linked list based on indexed memory.
714 * Example data structure:
716 * ILIST_ENTRY(uint16_t) next;
721 #define ILIST_ENTRY(type) \
723 type prev; /* Index of previous element. */ \
724 type next; /* Index of next element. */ \
727 #define ILIST_INSERT(pool, head, idx, elem, field) \
730 MLX5_ASSERT((elem) && (idx)); \
731 (elem)->field.next = *(head); \
732 (elem)->field.prev = 0; \
734 (peer) = mlx5_ipool_get(pool, *(head)); \
736 (peer)->field.prev = (idx); \
741 #define ILIST_REMOVE(pool, head, idx, elem, field) \
746 if ((elem)->field.prev) { \
747 (peer) = mlx5_ipool_get \
748 (pool, (elem)->field.prev); \
750 (peer)->field.next = (elem)->field.next;\
752 if ((elem)->field.next) { \
753 (peer) = mlx5_ipool_get \
754 (pool, (elem)->field.next); \
756 (peer)->field.prev = (elem)->field.prev;\
758 if (*(head) == (idx)) \
759 *(head) = (elem)->field.next; \
762 #define ILIST_FOREACH(pool, head, idx, elem, field) \
763 for ((idx) = (head), (elem) = \
764 (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem); \
765 idx = (elem)->field.next, (elem) = \
766 (idx) ? mlx5_ipool_get(pool, idx) : NULL)
768 /* Single index list. */
769 #define SILIST_ENTRY(type) \
771 type next; /* Index of next element. */ \
774 #define SILIST_INSERT(head, idx, elem, field) \
776 MLX5_ASSERT((elem) && (idx)); \
777 (elem)->field.next = *(head); \
781 #define SILIST_FOREACH(pool, head, idx, elem, field) \
782 for ((idx) = (head), (elem) = \
783 (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem); \
784 idx = (elem)->field.next, (elem) = \
785 (idx) ? mlx5_ipool_get(pool, idx) : NULL)
787 #endif /* RTE_PMD_MLX5_UTILS_H_ */