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_memory.h>
17 #include <rte_bitmap.h>
19 #include <mlx5_common.h>
21 #include "mlx5_defs.h"
24 /* Convert a bit number to the corresponding 64-bit mask */
25 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v))
27 /* Save and restore errno around argument evaluation. */
28 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0]))
30 extern int mlx5_logtype;
32 /* Generic printf()-like logging macro with automatic line feed. */
33 #define DRV_LOG(level, ...) \
34 PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \
35 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \
38 #define INFO(...) DRV_LOG(INFO, __VA_ARGS__)
39 #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__)
40 #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__)
42 /* Convenience macros for accessing mbuf fields. */
43 #define NEXT(m) ((m)->next)
44 #define DATA_LEN(m) ((m)->data_len)
45 #define PKT_LEN(m) ((m)->pkt_len)
46 #define DATA_OFF(m) ((m)->data_off)
47 #define SET_DATA_OFF(m, o) ((m)->data_off = (o))
48 #define NB_SEGS(m) ((m)->nb_segs)
49 #define PORT(m) ((m)->port)
51 /* Transpose flags. Useful to convert IBV to DPDK flags. */
52 #define TRANSPOSE(val, from, to) \
54 (((val) & (from)) / ((from) / (to))) : \
55 (((val) & (from)) * ((to) / (from))))
58 * For the case which data is linked with sequence increased index, the
59 * array table will be more efficiect than hash table once need to serarch
60 * one data entry in large numbers of entries. Since the traditional hash
61 * tables has fixed table size, when huge numbers of data saved to the hash
62 * table, it also comes lots of hash conflict.
64 * But simple array table also has fixed size, allocates all the needed
65 * memory at once will waste lots of memory. For the case don't know the
66 * exactly number of entries will be impossible to allocate the array.
68 * Then the multiple level table helps to balance the two disadvantages.
69 * Allocate a global high level table with sub table entries at first,
70 * the global table contains the sub table entries, and the sub table will
71 * be allocated only once the corresponding index entry need to be saved.
72 * e.g. for up to 32-bits index, three level table with 10-10-12 splitting,
73 * with sequence increased index, the memory grows with every 4K entries.
75 * The currently implementation introduces 10-10-12 32-bits splitting
76 * Three-Level table to help the cases which have millions of enties to
77 * save. The index entries can be addressed directly by the index, no
78 * search will be needed.q
81 /* L3 table global table define. */
82 #define MLX5_L3T_GT_OFFSET 22
83 #define MLX5_L3T_GT_SIZE (1 << 10)
84 #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1)
86 /* L3 table middle table define. */
87 #define MLX5_L3T_MT_OFFSET 12
88 #define MLX5_L3T_MT_SIZE (1 << 10)
89 #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1)
91 /* L3 table entry table define. */
92 #define MLX5_L3T_ET_OFFSET 0
93 #define MLX5_L3T_ET_SIZE (1 << 12)
94 #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1)
98 MLX5_L3T_TYPE_WORD = 0,
105 struct mlx5_indexed_pool;
107 /* Generic data struct. */
108 union mlx5_l3t_data {
115 /* L3 level table data structure. */
116 struct mlx5_l3t_level_tbl {
117 uint64_t ref_cnt; /* Table ref_cnt. */
118 void *tbl[]; /* Table array. */
121 /* L3 word entry table data structure. */
122 struct mlx5_l3t_entry_word {
123 uint32_t idx; /* Table index. */
124 uint64_t ref_cnt; /* Table ref_cnt. */
125 uint16_t entry[]; /* Entry array. */
128 /* L3 double word entry table data structure. */
129 struct mlx5_l3t_entry_dword {
130 uint32_t idx; /* Table index. */
131 uint64_t ref_cnt; /* Table ref_cnt. */
132 uint32_t entry[]; /* Entry array. */
135 /* L3 quad word entry table data structure. */
136 struct mlx5_l3t_entry_qword {
137 uint32_t idx; /* Table index. */
138 uint64_t ref_cnt; /* Table ref_cnt. */
139 uint64_t entry[]; /* Entry array. */
142 /* L3 pointer entry table data structure. */
143 struct mlx5_l3t_entry_ptr {
144 uint32_t idx; /* Table index. */
145 uint64_t ref_cnt; /* Table ref_cnt. */
146 void *entry[]; /* Entry array. */
149 /* L3 table data structure. */
150 struct mlx5_l3t_tbl {
151 enum mlx5_l3t_type type; /* Table type. */
152 struct mlx5_indexed_pool *eip;
153 /* Table index pool handles. */
154 struct mlx5_l3t_level_tbl *tbl; /* Global table index. */
158 * The indexed memory entry index is made up of trunk index and offset of
159 * the entry in the trunk. Since the entry index is 32 bits, in case user
160 * prefers to have small trunks, user can change the macro below to a big
161 * number which helps the pool contains more trunks with lots of entries
164 #define TRUNK_IDX_BITS 16
165 #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1)
166 #define TRUNK_INVALID TRUNK_MAX_IDX
167 #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS))
168 #ifdef RTE_LIBRTE_MLX5_DEBUG
172 struct mlx5_indexed_pool_config {
173 uint32_t size; /* Pool entry size. */
174 uint32_t trunk_size:22;
176 * Trunk entry number. Must be power of 2. It can be increased
177 * if trunk_grow enable. The trunk entry number increases with
178 * left shift grow_shift. Trunks with index are after grow_trunk
179 * will keep the entry number same with the last grow trunk.
181 uint32_t grow_trunk:4;
183 * Trunks with entry number increase in the pool. Set it to 0
184 * to make the pool works as trunk entry fixed pool. It works
185 * only if grow_shift is not 0.
187 uint32_t grow_shift:4;
189 * Trunk entry number increase shift value, stop after grow_trunk.
190 * It works only if grow_trunk is not 0.
192 uint32_t need_lock:1;
193 /* Lock is needed for multiple thread usage. */
194 uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
195 const char *type; /* Memory allocate type name. */
196 void *(*malloc)(const char *type, size_t size, unsigned int align,
198 /* User defined memory allocator. */
199 void (*free)(void *addr); /* User defined memory release. */
202 struct mlx5_indexed_trunk {
203 uint32_t idx; /* Trunk id. */
204 uint32_t prev; /* Previous free trunk in free list. */
205 uint32_t next; /* Next free trunk in free list. */
206 uint32_t free; /* Free entries available */
207 struct rte_bitmap *bmp;
208 uint8_t data[] __rte_cache_aligned; /* Entry data start. */
211 struct mlx5_indexed_pool {
212 struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */
213 rte_spinlock_t lock; /* Pool lock for multiple thread usage. */
214 uint32_t n_trunk_valid; /* Trunks allocated. */
215 uint32_t n_trunk; /* Trunk pointer array size. */
216 /* Dim of trunk pointer array. */
217 struct mlx5_indexed_trunk **trunks;
218 uint32_t free_list; /* Index to first free trunk. */
222 uint32_t trunk_avail;
223 uint32_t trunk_empty;
226 uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */
230 * Return logarithm of the nearest power of two above input value.
236 * Logarithm of the nearest power of two above input value.
238 static inline unsigned int
239 log2above(unsigned int v)
244 for (l = 0, r = 0; (v >> 1); ++l, v >>= 1)
249 /** Maximum size of string for naming the hlist table. */
250 #define MLX5_HLIST_NAMESIZE 32
253 * Structure of the entry in the hash list, user should define its own struct
254 * that contains this in order to store the data. The 'key' is 64-bits right
255 * now and its user's responsibility to guarantee there is no collision.
257 struct mlx5_hlist_entry {
258 LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
259 uint64_t key; /* user defined 'key', could be the hash signature. */
262 /** Structure for hash head. */
263 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
265 /** Type of function that is used to handle the data before freeing. */
266 typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx);
268 /** hash list table structure */
270 char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
271 /**< number of heads, need to be power of 2. */
273 /**< mask to get the index of the list heads. */
275 struct mlx5_hlist_head heads[]; /**< list head arrays. */
279 * Create a hash list table, the user can specify the list heads array size
280 * of the table, now the size should be a power of 2 in order to get better
281 * distribution for the entries. Each entry is a part of the whole data element
282 * and the caller should be responsible for the data element's allocation and
283 * cleanup / free. Key of each entry will be calculated with CRC in order to
284 * generate a little fairer distribution.
287 * Name of the hash list(optional).
289 * Heads array size of the hash list.
292 * Pointer of the hash list table created, NULL on failure.
294 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size);
297 * Search an entry matching the key.
300 * Pointer to the hast list table.
302 * Key for the searching entry.
305 * Pointer of the hlist entry if found, NULL otherwise.
307 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key);
310 * Insert an entry to the hash list table, the entry is only part of whole data
311 * element and a 64B key is used for matching. User should construct the key or
312 * give a calculated hash signature and guarantee there is no collision.
315 * Pointer to the hast list table.
317 * Entry to be inserted into the hash list table.
320 * - zero for success.
321 * - -EEXIST if the entry is already inserted.
323 int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
326 * Remove an entry from the hash list table. User should guarantee the validity
330 * Pointer to the hast list table. (not used)
332 * Entry to be removed from the hash list table.
334 void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused,
335 struct mlx5_hlist_entry *entry);
338 * Destroy the hash list table, all the entries already inserted into the lists
339 * will be handled by the callback function provided by the user (including
340 * free if needed) before the table is freed.
343 * Pointer to the hast list table.
345 * Callback function for each inserted entry when destroying the hash list.
347 * Common context parameter used by callback function for each entry.
349 void mlx5_hlist_destroy(struct mlx5_hlist *h,
350 mlx5_hlist_destroy_callback_fn cb, void *ctx);
353 * This function allocates non-initialized memory entry from pool.
354 * In NUMA systems, the memory entry allocated resides on the same
355 * NUMA socket as the core that calls this function.
357 * Memory entry is allocated from memory trunk, no alignment.
360 * Pointer to indexed memory entry pool.
361 * No initialization required.
363 * Pointer to memory to save allocated index.
364 * Memory index always positive value.
366 * - Pointer to the allocated memory entry.
367 * - NULL on error. Not enough memory, or invalid arguments.
369 void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
372 * This function allocates zero initialized memory entry from pool.
373 * In NUMA systems, the memory entry allocated resides on the same
374 * NUMA socket as the core that calls this function.
376 * Memory entry is allocated from memory trunk, no alignment.
379 * Pointer to indexed memory pool.
380 * No initialization required.
382 * Pointer to memory to save allocated index.
383 * Memory index always positive value.
385 * - Pointer to the allocated memory entry .
386 * - NULL on error. Not enough memory, or invalid arguments.
388 void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx);
391 * This function frees indexed memory entry to pool.
392 * Caller has to make sure that the index is allocated from same pool.
395 * Pointer to indexed memory pool.
397 * Allocated memory entry index.
399 void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx);
402 * This function returns pointer of indexed memory entry from index.
403 * Caller has to make sure that the index is valid, and allocated
407 * Pointer to indexed memory pool.
409 * Allocated memory index.
411 * - Pointer to indexed memory entry.
413 void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx);
416 * This function creates indexed memory pool.
417 * Caller has to configure the configuration accordingly.
420 * Pointer to indexed memory pool.
422 * Allocated memory index.
424 struct mlx5_indexed_pool *
425 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg);
428 * This function releases all resources of pool.
429 * Caller has to make sure that all indexes and memories allocated
430 * from this pool not referenced anymore.
433 * Pointer to indexed memory pool.
435 * - non-zero value on error.
438 int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool);
441 * This function dumps debug info of pool.
444 * Pointer to indexed memory pool.
446 void mlx5_ipool_dump(struct mlx5_indexed_pool *pool);
449 * This function allocates new empty Three-level table.
452 * The l3t can set as word, double word, quad word or pointer with index.
455 * - Pointer to the allocated l3t.
456 * - NULL on error. Not enough memory, or invalid arguments.
458 struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type);
461 * This function destroys Three-level table.
464 * Pointer to the l3t.
466 void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl);
469 * This function gets the index entry from Three-level table.
472 * Pointer to the l3t.
474 * Index to the entry.
476 * Pointer to the memory which saves the entry data.
477 * When function call returns 0, data contains the entry data get from
479 * When function call returns -1, data is not modified.
482 * 0 if success, -1 on error.
485 uint32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
486 union mlx5_l3t_data *data);
488 * This function clears the index entry from Three-level table.
491 * Pointer to the l3t.
493 * Index to the entry.
495 void mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx);
498 * This function gets the index entry from Three-level table.
501 * Pointer to the l3t.
503 * Index to the entry.
505 * Pointer to the memory which contains the entry data save to l3t.
508 * 0 if success, -1 on error.
510 uint32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
511 union mlx5_l3t_data *data);
514 * Macros for linked list based on indexed memory.
515 * Example data structure:
517 * ILIST_ENTRY(uint16_t) next;
522 #define ILIST_ENTRY(type) \
524 type prev; /* Index of previous element. */ \
525 type next; /* Index of next element. */ \
528 #define ILIST_INSERT(pool, head, idx, elem, field) \
531 MLX5_ASSERT((elem) && (idx)); \
532 (elem)->field.next = *(head); \
533 (elem)->field.prev = 0; \
535 (peer) = mlx5_ipool_get(pool, *(head)); \
537 (peer)->field.prev = (idx); \
542 #define ILIST_REMOVE(pool, head, idx, elem, field) \
547 if ((elem)->field.prev) { \
548 (peer) = mlx5_ipool_get \
549 (pool, (elem)->field.prev); \
551 (peer)->field.next = (elem)->field.next;\
553 if ((elem)->field.next) { \
554 (peer) = mlx5_ipool_get \
555 (pool, (elem)->field.next); \
557 (peer)->field.prev = (elem)->field.prev;\
559 if (*(head) == (idx)) \
560 *(head) = (elem)->field.next; \
563 #define ILIST_FOREACH(pool, head, idx, elem, field) \
564 for ((idx) = (head), (elem) = \
565 (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem); \
566 idx = (elem)->field.next, (elem) = \
567 (idx) ? mlx5_ipool_get(pool, idx) : NULL)
569 /* Single index list. */
570 #define SILIST_ENTRY(type) \
572 type next; /* Index of next element. */ \
575 #define SILIST_INSERT(head, idx, elem, field) \
577 MLX5_ASSERT((elem) && (idx)); \
578 (elem)->field.next = *(head); \
582 #define SILIST_FOREACH(pool, head, idx, elem, field) \
583 for ((idx) = (head), (elem) = \
584 (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem); \
585 idx = (elem)->field.next, (elem) = \
586 (idx) ? mlx5_ipool_get(pool, idx) : NULL)
588 #endif /* RTE_PMD_MLX5_UTILS_H_ */