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 #define MLX5_HLIST_DIRECT_KEY 0x0001 /* Use the key directly as hash index. */
11 #define MLX5_HLIST_WRITE_MOST 0x0002 /* List mostly used for append new. */
13 /** Maximum size of string for naming the hlist table. */
14 #define MLX5_HLIST_NAMESIZE 32
19 * Structure of the entry in the hash list, user should define its own struct
20 * that contains this in order to store the data. The 'key' is 64-bits right
21 * now and its user's responsibility to guarantee there is no collision.
23 struct mlx5_hlist_entry {
24 LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */
25 uint32_t idx; /* Bucket index the entry belongs to. */
26 uint32_t ref_cnt; /* Reference count. */
29 /** Structure for hash head. */
30 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
33 * Type of callback function for entry removal.
38 * The entry in the list.
40 typedef void (*mlx5_hlist_remove_cb)(struct mlx5_hlist *list,
41 struct mlx5_hlist_entry *entry);
44 * Type of function for user defined matching.
49 * The entry in the list.
53 * The pointer to new entry context.
56 * 0 if matching, non-zero number otherwise.
58 typedef int (*mlx5_hlist_match_cb)(struct mlx5_hlist *list,
59 struct mlx5_hlist_entry *entry,
60 uint64_t key, void *ctx);
63 * Type of function for user defined hash list entry creation.
68 * The key of the new entry.
70 * The pointer to new entry context.
73 * Pointer to allocated entry on success, NULL otherwise.
75 typedef struct mlx5_hlist_entry *(*mlx5_hlist_create_cb)
76 (struct mlx5_hlist *list,
77 uint64_t key, void *ctx);
79 /* Hash list bucket head. */
80 struct mlx5_hlist_bucket {
81 struct mlx5_hlist_head head; /* List head. */
82 rte_rwlock_t lock; /* Bucket lock. */
83 uint32_t gen_cnt; /* List modification will update generation count. */
84 } __rte_cache_aligned;
87 * Hash list table structure
89 * Entry in hash list could be reused if entry already exists, reference
90 * 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.
96 char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
97 /**< number of heads, need to be power of 2. */
99 uint32_t entry_sz; /**< Size of entry, used to allocate entry. */
100 /**< mask to get the index of the list heads. */
102 bool direct_key; /* Use the new entry key directly as hash index. */
103 bool write_most; /* List mostly used for append new or destroy. */
105 mlx5_hlist_create_cb cb_create; /**< entry create callback. */
106 mlx5_hlist_match_cb cb_match; /**< entry match callback. */
107 mlx5_hlist_remove_cb cb_remove; /**< entry remove callback. */
108 struct mlx5_hlist_bucket buckets[] __rte_cache_aligned;
109 /**< list bucket arrays. */
113 * Create a hash list table, the user can specify the list heads array size
114 * of the table, now the size should be a power of 2 in order to get better
115 * distribution for the entries. Each entry is a part of the whole data element
116 * and the caller should be responsible for the data element's allocation and
117 * cleanup / free. Key of each entry will be calculated with CRC in order to
118 * generate a little fairer distribution.
121 * Name of the hash list(optional).
123 * Heads array size of the hash list.
125 * Entry size to allocate if cb_create not specified.
127 * The hash list attribute flags.
129 * Callback function for entry create.
131 * Callback function for entry match.
133 * Callback function for entry destroy.
135 * Pointer of the hash list table created, NULL on failure.
138 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size,
139 uint32_t entry_size, uint32_t flags,
140 mlx5_hlist_create_cb cb_create,
141 mlx5_hlist_match_cb cb_match,
142 mlx5_hlist_remove_cb cb_destroy);
145 * Search an entry matching the key.
147 * Result returned might be destroyed by other thread, must use
148 * this function only in main thread.
151 * Pointer to the hast list table.
153 * Key for the searching entry.
155 * Common context parameter used by entry callback function.
158 * Pointer of the hlist entry if found, NULL otherwise.
161 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key,
165 * Insert an entry to the hash list table, the entry is only part of whole data
166 * element and a 64B key is used for matching. User should construct the key or
167 * give a calculated hash signature and guarantee there is no collision.
170 * Pointer to the hast list table.
172 * Entry to be inserted into the hash list table.
174 * Common context parameter used by callback function.
177 * registered entry on success, NULL otherwise
180 struct mlx5_hlist_entry *mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key,
184 * Remove an entry from the hash list table. User should guarantee the validity
188 * Pointer to the hast list table. (not used)
190 * Entry to be removed from the hash list table.
192 * 0 on entry removed, 1 on entry still referenced.
195 int mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
198 * Destroy the hash list table, all the entries already inserted into the lists
199 * will be handled by the callback function provided by the user (including
200 * free if needed) before the table is freed.
203 * Pointer to the hast list table.
206 void mlx5_hlist_destroy(struct mlx5_hlist *h);
208 #endif /* RTE_PMD_MLX5_COMMON_UTILS_H_ */