1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
5 #ifndef RTE_PMD_MLX5_COMMON_UTILS_H_
6 #define RTE_PMD_MLX5_COMMON_UTILS_H_
8 #include "mlx5_common.h"
10 /************************ mlx5 list *****************************/
12 /** Maximum size of string for naming. */
13 #define MLX5_NAME_SIZE 32
18 * Structure of the entry in the mlx5 list, user should define its own struct
19 * that contains this in order to store the data.
21 struct mlx5_list_entry {
22 LIST_ENTRY(mlx5_list_entry) next; /* Entry pointers in the list. */
23 uint32_t ref_cnt; /* 0 means, entry is invalid. */
25 struct mlx5_list_entry *gentry;
28 struct mlx5_list_cache {
29 LIST_HEAD(mlx5_list_head, mlx5_list_entry) h;
30 uint32_t inv_cnt; /* Invalid entries counter. */
31 } __rte_cache_aligned;
34 * Type of callback function for entry removal.
39 * The entry in the list.
41 typedef void (*mlx5_list_remove_cb)(struct mlx5_list *list,
42 struct mlx5_list_entry *entry);
45 * Type of function for user defined matching.
50 * The entry in the list.
52 * The pointer to new entry context.
55 * 0 if matching, non-zero number otherwise.
57 typedef int (*mlx5_list_match_cb)(struct mlx5_list *list,
58 struct mlx5_list_entry *entry, void *ctx);
60 typedef struct mlx5_list_entry *(*mlx5_list_clone_cb)
61 (struct mlx5_list *list,
62 struct mlx5_list_entry *entry, void *ctx);
64 typedef void (*mlx5_list_clone_free_cb)(struct mlx5_list *list,
65 struct mlx5_list_entry *entry);
68 * Type of function for user defined mlx5 list entry creation.
73 * The new allocated entry, NULL if list entry size unspecified,
74 * New entry has to be allocated in callback and return.
76 * The pointer to new entry context.
79 * Pointer of entry on success, NULL otherwise.
81 typedef struct mlx5_list_entry *(*mlx5_list_create_cb)
82 (struct mlx5_list *list,
83 struct mlx5_list_entry *entry,
87 * Linked mlx5 list structure.
89 * Entry in mlx5 list could be reused if entry already exists,
90 * reference count will increase and the existing entry returns.
92 * When destroy an entry from list, decrease reference count and only
93 * destroy when no further reference.
95 * Linked list is designed for limited number of entries,
96 * read mostly, less modification.
98 * For huge amount of entries, please consider hash list.
102 char name[MLX5_NAME_SIZE]; /**< Name of the mlx5 list. */
103 volatile uint32_t gen_cnt;
104 /* List modification will update generation count. */
105 volatile uint32_t count; /* number of entries in list. */
106 void *ctx; /* user objects target to callback. */
107 rte_rwlock_t lock; /* read/write lock. */
108 mlx5_list_create_cb cb_create; /**< entry create callback. */
109 mlx5_list_match_cb cb_match; /**< entry match callback. */
110 mlx5_list_remove_cb cb_remove; /**< entry remove callback. */
111 mlx5_list_clone_cb cb_clone; /**< entry clone callback. */
112 mlx5_list_clone_free_cb cb_clone_free;
113 struct mlx5_list_cache cache[RTE_MAX_LCORE + 1];
114 /* Lcore cache, last index is the global cache. */
118 * Create a mlx5 list.
121 * Pointer to the hast list table.
123 * Name of the mlx5 list.
125 * Pointer to the list context data.
127 * Callback function for entry create.
129 * Callback function for entry match.
131 * Callback function for entry remove.
133 * List pointer on success, otherwise NULL.
136 struct mlx5_list *mlx5_list_create(const char *name, void *ctx,
137 mlx5_list_create_cb cb_create,
138 mlx5_list_match_cb cb_match,
139 mlx5_list_remove_cb cb_remove,
140 mlx5_list_clone_cb cb_clone,
141 mlx5_list_clone_free_cb cb_clone_free);
144 * Search an entry matching the key.
146 * Result returned might be destroyed by other thread, must use
147 * this function only in main thread.
150 * Pointer to the mlx5 list.
152 * Common context parameter used by entry callback function.
155 * Pointer of the list entry if found, NULL otherwise.
158 struct mlx5_list_entry *mlx5_list_lookup(struct mlx5_list *list,
162 * Reuse or create an entry to the mlx5 list.
165 * Pointer to the hast list table.
167 * Common context parameter used by callback function.
170 * registered entry on success, NULL otherwise
173 struct mlx5_list_entry *mlx5_list_register(struct mlx5_list *list,
177 * Remove an entry from the mlx5 list.
179 * User should guarantee the validity of the entry.
182 * Pointer to the hast list.
184 * Entry to be removed from the mlx5 list table.
186 * 0 on entry removed, 1 on entry still referenced.
189 int mlx5_list_unregister(struct mlx5_list *list,
190 struct mlx5_list_entry *entry);
193 * Destroy the mlx5 list.
196 * Pointer to the mlx5 list.
199 void mlx5_list_destroy(struct mlx5_list *list);
202 * Get entry number from the mlx5 list.
205 * Pointer to the hast list.
207 * mlx5 list entry number.
211 mlx5_list_get_entry_num(struct mlx5_list *list);
213 /************************ Hash list *****************************/
215 #define MLX5_HLIST_DIRECT_KEY 0x0001 /* Use the key directly as hash index. */
216 #define MLX5_HLIST_WRITE_MOST 0x0002 /* List mostly used for append new. */
218 /** Maximum size of string for naming the hlist table. */
219 #define MLX5_HLIST_NAMESIZE 32
224 * Structure of the entry in the hash list, user should define its own struct
225 * that contains this in order to store the data. The 'key' is 64-bits right
226 * now and its user's responsibility to guarantee there is no collision.
228 struct mlx5_hlist_entry {
229 LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
230 uint32_t idx; /* Bucket index the entry belongs to. */
231 uint32_t ref_cnt; /* Reference count. */
234 /** Structure for hash head. */
235 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
238 * Type of callback function for entry removal.
243 * The entry in the list.
245 typedef void (*mlx5_hlist_remove_cb)(struct mlx5_hlist *list,
246 struct mlx5_hlist_entry *entry);
249 * Type of function for user defined matching.
254 * The entry in the list.
258 * The pointer to new entry context.
261 * 0 if matching, non-zero number otherwise.
263 typedef int (*mlx5_hlist_match_cb)(struct mlx5_hlist *list,
264 struct mlx5_hlist_entry *entry,
265 uint64_t key, void *ctx);
268 * Type of function for user defined hash list entry creation.
273 * The key of the new entry.
275 * The pointer to new entry context.
278 * Pointer to allocated entry on success, NULL otherwise.
280 typedef struct mlx5_hlist_entry *(*mlx5_hlist_create_cb)
281 (struct mlx5_hlist *list,
282 uint64_t key, void *ctx);
284 /* Hash list bucket head. */
285 struct mlx5_hlist_bucket {
286 struct mlx5_hlist_head head; /* List head. */
287 rte_rwlock_t lock; /* Bucket lock. */
288 uint32_t gen_cnt; /* List modification will update generation count. */
289 } __rte_cache_aligned;
292 * Hash list table structure
294 * Entry in hash list could be reused if entry already exists, reference
295 * count will increase and the existing entry returns.
297 * When destroy an entry from list, decrease reference count and only
298 * destroy when no further reference.
301 char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
302 /**< number of heads, need to be power of 2. */
304 uint32_t entry_sz; /**< Size of entry, used to allocate entry. */
305 /**< mask to get the index of the list heads. */
307 bool direct_key; /* Use the new entry key directly as hash index. */
308 bool write_most; /* List mostly used for append new or destroy. */
310 mlx5_hlist_create_cb cb_create; /**< entry create callback. */
311 mlx5_hlist_match_cb cb_match; /**< entry match callback. */
312 mlx5_hlist_remove_cb cb_remove; /**< entry remove callback. */
313 struct mlx5_hlist_bucket buckets[] __rte_cache_aligned;
314 /**< list bucket arrays. */
318 * Create a hash list table, the user can specify the list heads array size
319 * of the table, now the size should be a power of 2 in order to get better
320 * distribution for the entries. Each entry is a part of the whole data element
321 * and the caller should be responsible for the data element's allocation and
322 * cleanup / free. Key of each entry will be calculated with CRC in order to
323 * generate a little fairer distribution.
326 * Name of the hash list(optional).
328 * Heads array size of the hash list.
330 * Entry size to allocate if cb_create not specified.
332 * The hash list attribute flags.
334 * Callback function for entry create.
336 * Callback function for entry match.
338 * Callback function for entry destroy.
340 * Pointer of the hash list table created, NULL on failure.
343 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size,
344 uint32_t entry_size, uint32_t flags,
345 mlx5_hlist_create_cb cb_create,
346 mlx5_hlist_match_cb cb_match,
347 mlx5_hlist_remove_cb cb_destroy);
350 * Search an entry matching the key.
352 * Result returned might be destroyed by other thread, must use
353 * this function only in main thread.
356 * Pointer to the hast list table.
358 * Key for the searching entry.
360 * Common context parameter used by entry callback function.
363 * Pointer of the hlist entry if found, NULL otherwise.
366 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key,
370 * Insert an entry to the hash list table, the entry is only part of whole data
371 * element and a 64B key is used for matching. User should construct the key or
372 * give a calculated hash signature and guarantee there is no collision.
375 * Pointer to the hast list table.
377 * Entry to be inserted into the hash list table.
379 * Common context parameter used by callback function.
382 * registered entry on success, NULL otherwise
385 struct mlx5_hlist_entry *mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key,
389 * Remove an entry from the hash list table. User should guarantee the validity
393 * Pointer to the hast list table. (not used)
395 * Entry to be removed from the hash list table.
397 * 0 on entry removed, 1 on entry still referenced.
400 int mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
403 * Destroy the hash list table, all the entries already inserted into the lists
404 * will be handled by the callback function provided by the user (including
405 * free if needed) before the table is freed.
408 * Pointer to the hast list table.
411 void mlx5_hlist_destroy(struct mlx5_hlist *h);
413 #endif /* RTE_PMD_MLX5_COMMON_UTILS_H_ */