common/mlx5: move list utility from net driver
[dpdk.git] / drivers / common / mlx5 / mlx5_common_utils.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4
5 #ifndef RTE_PMD_MLX5_COMMON_UTILS_H_
6 #define RTE_PMD_MLX5_COMMON_UTILS_H_
7
8 #include "mlx5_common.h"
9
10 /************************ mlx5 list *****************************/
11
12 /** Maximum size of string for naming. */
13 #define MLX5_NAME_SIZE                  32
14
15 struct mlx5_list;
16
17 /**
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.
20  */
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. */
24         uint32_t lcore_idx;
25         struct mlx5_list_entry *gentry;
26 };
27
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;
32
33 /**
34  * Type of callback function for entry removal.
35  *
36  * @param list
37  *   The mlx5 list.
38  * @param entry
39  *   The entry in the list.
40  */
41 typedef void (*mlx5_list_remove_cb)(struct mlx5_list *list,
42                                      struct mlx5_list_entry *entry);
43
44 /**
45  * Type of function for user defined matching.
46  *
47  * @param list
48  *   The mlx5 list.
49  * @param entry
50  *   The entry in the list.
51  * @param ctx
52  *   The pointer to new entry context.
53  *
54  * @return
55  *   0 if matching, non-zero number otherwise.
56  */
57 typedef int (*mlx5_list_match_cb)(struct mlx5_list *list,
58                                    struct mlx5_list_entry *entry, void *ctx);
59
60 typedef struct mlx5_list_entry *(*mlx5_list_clone_cb)
61                                  (struct mlx5_list *list,
62                                   struct mlx5_list_entry *entry, void *ctx);
63
64 typedef void (*mlx5_list_clone_free_cb)(struct mlx5_list *list,
65                                          struct mlx5_list_entry *entry);
66
67 /**
68  * Type of function for user defined mlx5 list entry creation.
69  *
70  * @param list
71  *   The mlx5 list.
72  * @param entry
73  *   The new allocated entry, NULL if list entry size unspecified,
74  *   New entry has to be allocated in callback and return.
75  * @param ctx
76  *   The pointer to new entry context.
77  *
78  * @return
79  *   Pointer of entry on success, NULL otherwise.
80  */
81 typedef struct mlx5_list_entry *(*mlx5_list_create_cb)
82                                  (struct mlx5_list *list,
83                                   struct mlx5_list_entry *entry,
84                                   void *ctx);
85
86 /**
87  * Linked mlx5 list structure.
88  *
89  * Entry in mlx5 list could be reused if entry already exists,
90  * reference count will increase and the existing entry returns.
91  *
92  * When destroy an entry from list, decrease reference count and only
93  * destroy when no further reference.
94  *
95  * Linked list is designed for limited number of entries,
96  * read mostly, less modification.
97  *
98  * For huge amount of entries, please consider hash list.
99  *
100  */
101 struct mlx5_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. */
115 };
116
117 /**
118  * Create a mlx5 list.
119  *
120  * @param list
121  *   Pointer to the hast list table.
122  * @param name
123  *   Name of the mlx5 list.
124  * @param ctx
125  *   Pointer to the list context data.
126  * @param cb_create
127  *   Callback function for entry create.
128  * @param cb_match
129  *   Callback function for entry match.
130  * @param cb_remove
131  *   Callback function for entry remove.
132  * @return
133  *   List pointer on success, otherwise NULL.
134  */
135 __rte_internal
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);
142
143 /**
144  * Search an entry matching the key.
145  *
146  * Result returned might be destroyed by other thread, must use
147  * this function only in main thread.
148  *
149  * @param list
150  *   Pointer to the mlx5 list.
151  * @param ctx
152  *   Common context parameter used by entry callback function.
153  *
154  * @return
155  *   Pointer of the list entry if found, NULL otherwise.
156  */
157 __rte_internal
158 struct mlx5_list_entry *mlx5_list_lookup(struct mlx5_list *list,
159                                            void *ctx);
160
161 /**
162  * Reuse or create an entry to the mlx5 list.
163  *
164  * @param list
165  *   Pointer to the hast list table.
166  * @param ctx
167  *   Common context parameter used by callback function.
168  *
169  * @return
170  *   registered entry on success, NULL otherwise
171  */
172 __rte_internal
173 struct mlx5_list_entry *mlx5_list_register(struct mlx5_list *list,
174                                              void *ctx);
175
176 /**
177  * Remove an entry from the mlx5 list.
178  *
179  * User should guarantee the validity of the entry.
180  *
181  * @param list
182  *   Pointer to the hast list.
183  * @param entry
184  *   Entry to be removed from the mlx5 list table.
185  * @return
186  *   0 on entry removed, 1 on entry still referenced.
187  */
188 __rte_internal
189 int mlx5_list_unregister(struct mlx5_list *list,
190                           struct mlx5_list_entry *entry);
191
192 /**
193  * Destroy the mlx5 list.
194  *
195  * @param list
196  *   Pointer to the mlx5 list.
197  */
198 __rte_internal
199 void mlx5_list_destroy(struct mlx5_list *list);
200
201 /**
202  * Get entry number from the mlx5 list.
203  *
204  * @param list
205  *   Pointer to the hast list.
206  * @return
207  *   mlx5 list entry number.
208  */
209 __rte_internal
210 uint32_t
211 mlx5_list_get_entry_num(struct mlx5_list *list);
212
213 /************************ Hash list *****************************/
214
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. */
217
218 /** Maximum size of string for naming the hlist table. */
219 #define MLX5_HLIST_NAMESIZE                     32
220
221 struct mlx5_hlist;
222
223 /**
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.
227  */
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. */
232 };
233
234 /** Structure for hash head. */
235 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry);
236
237 /**
238  * Type of callback function for entry removal.
239  *
240  * @param list
241  *   The hash list.
242  * @param entry
243  *   The entry in the list.
244  */
245 typedef void (*mlx5_hlist_remove_cb)(struct mlx5_hlist *list,
246                                      struct mlx5_hlist_entry *entry);
247
248 /**
249  * Type of function for user defined matching.
250  *
251  * @param list
252  *   The hash list.
253  * @param entry
254  *   The entry in the list.
255  * @param key
256  *   The new entry key.
257  * @param ctx
258  *   The pointer to new entry context.
259  *
260  * @return
261  *   0 if matching, non-zero number otherwise.
262  */
263 typedef int (*mlx5_hlist_match_cb)(struct mlx5_hlist *list,
264                                    struct mlx5_hlist_entry *entry,
265                                    uint64_t key, void *ctx);
266
267 /**
268  * Type of function for user defined hash list entry creation.
269  *
270  * @param list
271  *   The hash list.
272  * @param key
273  *   The key of the new entry.
274  * @param ctx
275  *   The pointer to new entry context.
276  *
277  * @return
278  *   Pointer to allocated entry on success, NULL otherwise.
279  */
280 typedef struct mlx5_hlist_entry *(*mlx5_hlist_create_cb)
281                                   (struct mlx5_hlist *list,
282                                    uint64_t key, void *ctx);
283
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;
290
291 /**
292  * Hash list table structure
293  *
294  * Entry in hash list could be reused if entry already exists, reference
295  * count will increase and the existing entry returns.
296  *
297  * When destroy an entry from list, decrease reference count and only
298  * destroy when no further reference.
299  */
300 struct mlx5_hlist {
301         char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */
302         /**< number of heads, need to be power of 2. */
303         uint32_t table_sz;
304         uint32_t entry_sz; /**< Size of entry, used to allocate entry. */
305         /**< mask to get the index of the list heads. */
306         uint32_t mask;
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. */
309         void *ctx;
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. */
315 };
316
317 /**
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.
324  *
325  * @param name
326  *   Name of the hash list(optional).
327  * @param size
328  *   Heads array size of the hash list.
329  * @param entry_size
330  *   Entry size to allocate if cb_create not specified.
331  * @param flags
332  *   The hash list attribute flags.
333  * @param cb_create
334  *   Callback function for entry create.
335  * @param cb_match
336  *   Callback function for entry match.
337  * @param cb_destroy
338  *   Callback function for entry destroy.
339  * @return
340  *   Pointer of the hash list table created, NULL on failure.
341  */
342 __rte_internal
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);
348
349 /**
350  * Search an entry matching the key.
351  *
352  * Result returned might be destroyed by other thread, must use
353  * this function only in main thread.
354  *
355  * @param h
356  *   Pointer to the hast list table.
357  * @param key
358  *   Key for the searching entry.
359  * @param ctx
360  *   Common context parameter used by entry callback function.
361  *
362  * @return
363  *   Pointer of the hlist entry if found, NULL otherwise.
364  */
365 __rte_internal
366 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key,
367                                            void *ctx);
368
369 /**
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.
373  *
374  * @param h
375  *   Pointer to the hast list table.
376  * @param entry
377  *   Entry to be inserted into the hash list table.
378  * @param ctx
379  *   Common context parameter used by callback function.
380  *
381  * @return
382  *   registered entry on success, NULL otherwise
383  */
384 __rte_internal
385 struct mlx5_hlist_entry *mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key,
386                                              void *ctx);
387
388 /**
389  * Remove an entry from the hash list table. User should guarantee the validity
390  * of the entry.
391  *
392  * @param h
393  *   Pointer to the hast list table. (not used)
394  * @param entry
395  *   Entry to be removed from the hash list table.
396  * @return
397  *   0 on entry removed, 1 on entry still referenced.
398  */
399 __rte_internal
400 int mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry);
401
402 /**
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.
406  *
407  * @param h
408  *   Pointer to the hast list table.
409  */
410 __rte_internal
411 void mlx5_hlist_destroy(struct mlx5_hlist *h);
412
413 #endif /* RTE_PMD_MLX5_COMMON_UTILS_H_ */