b7787678f5b131e065925f1d07bd6b97752bd81e
[dpdk.git] / lib / librte_member / rte_member.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 /**
6  * @file
7  *
8  * RTE Membership Library
9  *
10  * The Membership Library is an extension and generalization of a traditional
11  * filter (for example Bloom Filter and cuckoo filter) structure that has
12  * multiple usages in a variety of workloads and applications. The library is
13  * used to test if a key belongs to certain sets. Two types of such
14  * "set-summary" structures are implemented: hash-table based (HT) and vector
15  * bloom filter (vBF). For HT setsummary, two subtypes or modes are available,
16  * cache and non-cache modes. The table below summarize some properties of
17  * the different implementations.
18  *
19  * @warning
20  * @b EXPERIMENTAL: this API may change without prior notice
21  */
22
23 /**
24  * <!--
25  * +==========+=====================+================+=========================+
26  * |   type   |      vbf            |     HT-cache   |     HT-non-cache        |
27  * +==========+=====================+==========================================+
28  * |structure |  bloom-filter array |  hash-table like without storing key     |
29  * +----------+---------------------+------------------------------------------+
30  * |set id    | limited by bf count |           [1, 0x7fff]                    |
31  * |          | up to 32.           |                                          |
32  * +----------+---------------------+------------------------------------------+
33  * |usages &  | small set range,    | can delete,    | cache most recent keys, |
34  * |properties| user-specified      | big set range, | have both false-positive|
35  * |          | false-positive rate,| small false    | and false-negative      |
36  * |          | no deletion support.| positive depend| depend on table size,   |
37  * |          |                     | on table size, | automatic overwritten.  |
38  * |          |                     | new key does   |                         |
39  * |          |                     | not overwrite  |                         |
40  * |          |                     | existing key.  |                         |
41  * +----------+---------------------+----------------+-------------------------+
42  * -->
43  */
44
45 #ifndef _RTE_MEMBER_H_
46 #define _RTE_MEMBER_H_
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 #include <stdint.h>
53
54 #include <rte_common.h>
55
56 /** The set ID type that stored internally in hash table based set summary. */
57 typedef uint16_t member_set_t;
58 /** Invalid set ID used to mean no match found. */
59 #define RTE_MEMBER_NO_MATCH 0
60 /** Maximum size of hash table that can be created. */
61 #define RTE_MEMBER_ENTRIES_MAX (1 << 30)
62 /** Maximum number of keys that can be searched as a bulk */
63 #define RTE_MEMBER_LOOKUP_BULK_MAX 64
64 /** Entry count per bucket in hash table based mode. */
65 #define RTE_MEMBER_BUCKET_ENTRIES 16
66 /** Maximum number of characters in setsum name. */
67 #define RTE_MEMBER_NAMESIZE 32
68
69 /** @internal Hash function used by membership library. */
70 #if defined(RTE_ARCH_X86) || defined(RTE_MACHINE_CPUFLAG_CRC32)
71 #include <rte_hash_crc.h>
72 #define MEMBER_HASH_FUNC       rte_hash_crc
73 #else
74 #include <rte_jhash.h>
75 #define MEMBER_HASH_FUNC       rte_jhash
76 #endif
77
78 extern int librte_member_logtype;
79
80 #define RTE_MEMBER_LOG(level, ...) \
81         rte_log(RTE_LOG_ ## level, \
82                 librte_member_logtype, \
83                 RTE_FMT("%s(): " RTE_FMT_HEAD(__VA_ARGS__,), \
84                         __func__, \
85                         RTE_FMT_TAIL(__VA_ARGS__,)))
86
87 /** @internal setsummary structure. */
88 struct rte_member_setsum;
89
90 /**
91  * @warning
92  * @b EXPERIMENTAL: this API may change without prior notice
93  *
94  * Parameter struct used to create set summary
95  */
96 struct rte_member_parameters;
97
98 /**
99  * @warning
100  * @b EXPERIMENTAL: this API may change without prior notice
101  *
102  * Define different set summary types
103  */
104 enum rte_member_setsum_type {
105         RTE_MEMBER_TYPE_HT = 0,  /**< Hash table based set summary. */
106         RTE_MEMBER_TYPE_VBF,     /**< Vector of bloom filters. */
107         RTE_MEMBER_NUM_TYPE
108 };
109
110 /** @internal compare function for different arch. */
111 enum rte_member_sig_compare_function {
112         RTE_MEMBER_COMPARE_SCALAR = 0,
113         RTE_MEMBER_COMPARE_AVX2,
114         RTE_MEMBER_COMPARE_NUM
115 };
116
117 /** @internal setsummary structure. */
118 struct rte_member_setsum {
119         enum rte_member_setsum_type type; /* Type of the set summary. */
120         uint32_t key_len;               /* Length of key. */
121         uint32_t prim_hash_seed;        /* Primary hash function seed. */
122         uint32_t sec_hash_seed;         /* Secondary hash function seed. */
123
124         /* Hash table based. */
125         uint32_t bucket_cnt;            /* Number of buckets. */
126         uint32_t bucket_mask;           /* Bit mask to get bucket index. */
127         /* For runtime selecting AVX, scalar, etc for signature comparison. */
128         enum rte_member_sig_compare_function sig_cmp_fn;
129         uint8_t cache;                  /* If it is cache mode for ht based. */
130
131         /* Vector bloom filter. */
132         uint32_t num_set;               /* Number of set (bf) in vbf. */
133         uint32_t bits;                  /* Number of bits in each bf. */
134         uint32_t bit_mask;      /* Bit mask to get bit location in bf. */
135         uint32_t num_hashes;    /* Number of hash values to index bf. */
136
137         uint32_t mul_shift;  /* vbf internal variable used during bit test. */
138         uint32_t div_shift;  /* vbf internal variable used during bit test. */
139
140         void *table;    /* This is the handler of hash table or vBF array. */
141
142
143         /* Second cache line should start here. */
144         uint32_t socket_id;          /* NUMA Socket ID for memory. */
145         char name[RTE_MEMBER_NAMESIZE]; /* Name of this set summary. */
146 } __rte_cache_aligned;
147
148 /**
149  * @warning
150  * @b EXPERIMENTAL: this API may change without prior notice
151  *
152  * Parameters used when create the set summary table. Currently user can
153  * specify two types of setsummary: HT based and vBF. For HT based, user can
154  * specify cache or non-cache mode. Here is a table to describe some differences
155  *
156  */
157 struct rte_member_parameters {
158         const char *name;                       /**< Name of the hash. */
159
160         /**
161          * User to specify the type of the setsummary from one of
162          * rte_member_setsum_type.
163          *
164          * HT based setsummary is implemented like a hash table. User should use
165          * this type when there are many sets.
166          *
167          * vBF setsummary is a vector of bloom filters. It is used when number
168          * of sets is not big (less than 32 for current implementation).
169          */
170         enum rte_member_setsum_type type;
171
172         /**
173          * is_cache is only used for HT based setsummary.
174          *
175          * If it is HT based setsummary, user to specify the subtype or mode
176          * of the setsummary. It could be cache, or non-cache mode.
177          * Set is_cache to be 1 if to use as cache mode.
178          *
179          * For cache mode, keys can be evicted out of the HT setsummary. Keys
180          * with the same signature and map to the same bucket
181          * will overwrite each other in the setsummary table.
182          * This mode is useful for the case that the set-summary only
183          * needs to keep record of the recently inserted keys. Both
184          * false-negative and false-positive could happen.
185          *
186          * For non-cache mode, keys cannot be evicted out of the cache. So for
187          * this mode the setsummary will become full eventually. Keys with the
188          * same signature but map to the same bucket will still occupy multiple
189          * entries. This mode does not give false-negative result.
190          */
191         uint8_t is_cache;
192
193         /**
194          * For HT setsummary, num_keys equals to the number of entries of the
195          * table. When the number of keys inserted in the HT setsummary
196          * approaches this number, eviction could happen. For cache mode,
197          * keys could be evicted out of the table. For non-cache mode, keys will
198          * be evicted to other buckets like cuckoo hash. The table will also
199          * likely to become full before the number of inserted keys equal to the
200          * total number of entries.
201          *
202          * For vBF, num_keys equal to the expected number of keys that will
203          * be inserted into the vBF. The implementation assumes the keys are
204          * evenly distributed to each BF in vBF. This is used to calculate the
205          * number of bits we need for each BF. User does not specify the size of
206          * each BF directly because the optimal size depends on the num_keys
207          * and false positive rate.
208          */
209         uint32_t num_keys;
210
211         /**
212          * The length of key is used for hash calculation. Since key is not
213          * stored in set-summary, large key does not require more memory space.
214          */
215         uint32_t key_len;
216
217         /**
218          * num_set is only used for vBF, but not used for HT setsummary.
219          *
220          * num_set is equal to the number of BFs in vBF. For current
221          * implementation, it only supports 1,2,4,8,16,32 BFs in one vBF set
222          * summary. If other number of sets are needed, for example 5, the user
223          * should allocate the minimum available value that larger than 5,
224          * which is 8.
225          */
226         uint32_t num_set;
227
228         /**
229          * false_positive_rate is only used for vBF, but not used for HT
230          * setsummary.
231          *
232          * For vBF, false_positive_rate is the user-defined false positive rate
233          * given expected number of inserted keys (num_keys). It is used to
234          * calculate the total number of bits for each BF, and the number of
235          * hash values used during lookup and insertion. For details please
236          * refer to vBF implementation and membership library documentation.
237          *
238          * For HT, This parameter is not directly set by users.
239          * HT setsummary's false positive rate is in the order of:
240          * false_pos = (1/bucket_count)*(1/2^16), since we use 16-bit signature.
241          * This is because two keys needs to map to same bucket and same
242          * signature to have a collision (false positive). bucket_count is equal
243          * to number of entries (num_keys) divided by entry count per bucket
244          * (RTE_MEMBER_BUCKET_ENTRIES). Thus, the false_positive_rate is not
245          * directly set by users for HT mode.
246          */
247         float false_positive_rate;
248
249         /**
250          * We use two seeds to calculate two independent hashes for each key.
251          *
252          * For HT type, one hash is used as signature, and the other is used
253          * for bucket location.
254          * For vBF type, these two hashes and their combinations are used as
255          * hash locations to index the bit array.
256          */
257         uint32_t prim_hash_seed;
258
259         /**
260          * The secondary seed should be a different value from the primary seed.
261          */
262         uint32_t sec_hash_seed;
263
264         int socket_id;                  /**< NUMA Socket ID for memory. */
265 };
266
267 /**
268  * @warning
269  * @b EXPERIMENTAL: this API may change without prior notice
270  *
271  * Find an existing set-summary and return a pointer to it.
272  *
273  * @param name
274  *   Name of the set-summary.
275  * @return
276  *   Pointer to the set-summary or NULL if object not found
277  *   with rte_errno set appropriately. Possible rte_errno values include:
278  *    - ENOENT - value not available for return
279  */
280 struct rte_member_setsum *
281 rte_member_find_existing(const char *name);
282
283 /**
284  * @warning
285  * @b EXPERIMENTAL: this API may change without prior notice
286  *
287  * Create set-summary (SS).
288  *
289  * @param params
290  *   Parameters to initialize the setsummary.
291  * @return
292  *   Return the pointer to the setsummary.
293  *   Return value is NULL if the creation failed.
294  */
295 struct rte_member_setsum *
296 rte_member_create(const struct rte_member_parameters *params);
297
298 /**
299  * @warning
300  * @b EXPERIMENTAL: this API may change without prior notice
301  *
302  * Lookup key in set-summary (SS).
303  * Single key lookup and return as soon as the first match found
304  *
305  * @param setsum
306  *   Pointer of a setsummary.
307  * @param key
308  *   Pointer of the key to be looked up.
309  * @param set_id
310  *   Output the set id matches the key.
311  * @return
312  *   Return 1 for found a match and 0 for not found a match.
313  */
314 int
315 rte_member_lookup(const struct rte_member_setsum *setsum, const void *key,
316                         member_set_t *set_id);
317
318 /**
319  * @warning
320  * @b EXPERIMENTAL: this API may change without prior notice
321  *
322  * Lookup bulk of keys in set-summary (SS).
323  * Each key lookup returns as soon as the first match found
324  *
325  * @param setsum
326  *   Pointer of a setsummary.
327  * @param keys
328  *   Pointer of the bulk of keys to be looked up.
329  * @param num_keys
330  *   Number of keys that will be lookup.
331  * @param set_ids
332  *   Output set ids for all the keys to this array.
333  *   User should preallocate array that can contain all results, which size is
334  *   the num_keys.
335  * @return
336  *   The number of keys that found a match.
337  */
338 int
339 rte_member_lookup_bulk(const struct rte_member_setsum *setsum,
340                         const void **keys, uint32_t num_keys,
341                         member_set_t *set_ids);
342
343 /**
344  * @warning
345  * @b EXPERIMENTAL: this API may change without prior notice
346  *
347  * Lookup a key in set-summary (SS) for multiple matches.
348  * The key lookup will find all matched entries (multiple match).
349  * Note that for cache mode of HT, each key can have at most one match. This is
350  * because keys with same signature that maps to same bucket will overwrite
351  * each other. So multi-match lookup should be used for vBF and non-cache HT.
352  *
353  * @param setsum
354  *   Pointer of a set-summary.
355  * @param key
356  *   Pointer of the key that to be looked up.
357  * @param max_match_per_key
358  *   User specified maximum number of matches for each key. The function returns
359  *   as soon as this number of matches found for the key.
360  * @param set_id
361  *   Output set ids for all the matches of the key. User needs to preallocate
362  *   the array that can contain max_match_per_key number of results.
363  * @return
364  *   The number of matches that found for the key.
365  *   For cache mode HT set-summary, the number should be at most 1.
366  */
367 int
368 rte_member_lookup_multi(const struct rte_member_setsum *setsum,
369                 const void *key, uint32_t max_match_per_key,
370                 member_set_t *set_id);
371
372 /**
373  * @warning
374  * @b EXPERIMENTAL: this API may change without prior notice
375  *
376  * Lookup a bulk of keys in set-summary (SS) for multiple matches each key.
377  * Each key lookup will find all matched entries (multiple match).
378  * Note that for cache mode HT, each key can have at most one match. So
379  * multi-match function is mainly used for vBF and non-cache mode HT.
380  *
381  * @param setsum
382  *   Pointer of a setsummary.
383  * @param keys
384  *   Pointer of the keys to be looked up.
385  * @param num_keys
386  *   The number of keys that will be lookup.
387  * @param max_match_per_key
388  *   The possible maximum number of matches for each key.
389  * @param match_count
390  *   Output the number of matches for each key in an array.
391  * @param set_ids
392  *   Return set ids for all the matches of all keys. Users pass in a
393  *   preallocated 2D array with first dimension as key index and second
394  *   dimension as match index. For example set_ids[bulk_size][max_match_per_key]
395  * @return
396  *   The number of keys that found one or more matches in the set-summary.
397  */
398 int
399 rte_member_lookup_multi_bulk(const struct rte_member_setsum *setsum,
400                 const void **keys, uint32_t num_keys,
401                 uint32_t max_match_per_key,
402                 uint32_t *match_count,
403                 member_set_t *set_ids);
404
405 /**
406  * @warning
407  * @b EXPERIMENTAL: this API may change without prior notice
408  *
409  * Insert key into set-summary (SS).
410  *
411  * @param setsum
412  *   Pointer of a set-summary.
413  * @param key
414  *   Pointer of the key to be added.
415  * @param set_id
416  *   The set id associated with the key that needs to be added. Different mode
417  *   supports different set_id ranges. 0 cannot be used as set_id since
418  *   RTE_MEMBER_NO_MATCH by default is set as 0.
419  *   For HT mode, the set_id has range as [1, 0x7FFF], MSB is reserved.
420  *   For vBF mode the set id is limited by the num_set parameter when create
421  *   the set-summary.
422  * @return
423  *   HT (cache mode) and vBF should never fail unless the set_id is not in the
424  *   valid range. In such case -EINVAL is returned.
425  *   For HT (non-cache mode) it could fail with -ENOSPC error code when table is
426  *   full.
427  *   For success it returns different values for different modes to provide
428  *   extra information for users.
429  *   Return 0 for HT (cache mode) if the add does not cause
430  *   eviction, return 1 otherwise. Return 0 for non-cache mode if success,
431  *   -ENOSPC for full, and 1 if cuckoo eviction happens.
432  *   Always returns 0 for vBF mode.
433  */
434 int
435 rte_member_add(const struct rte_member_setsum *setsum, const void *key,
436                         member_set_t set_id);
437
438 /**
439  * @warning
440  * @b EXPERIMENTAL: this API may change without prior notice
441  *
442  * De-allocate memory used by set-summary.
443  *
444  * @param setsum
445  *   Pointer to the set summary.
446  */
447 void
448 rte_member_free(struct rte_member_setsum *setsum);
449
450 /**
451  * @warning
452  * @b EXPERIMENTAL: this API may change without prior notice
453  *
454  * Reset the set-summary tables. E.g. reset bits to be 0 in BF,
455  * reset set_id in each entry to be RTE_MEMBER_NO_MATCH in HT based SS.
456  *
457  * @param setsum
458  *   Pointer to the set-summary.
459  */
460 void
461 rte_member_reset(const struct rte_member_setsum *setsum);
462
463 /**
464  * @warning
465  * @b EXPERIMENTAL: this API may change without prior notice
466  *
467  * Delete items from the set-summary. Note that vBF does not support deletion
468  * in current implementation. For vBF, error code of -EINVAL will be returned.
469  *
470  * @param setsum
471  *   Pointer to the set-summary.
472  * @param key
473  *   Pointer of the key to be deleted.
474  * @param set_id
475  *   For HT mode, we need both key and its corresponding set_id to
476  *   properly delete the key. Without set_id, we may delete other keys with the
477  *   same signature.
478  * @return
479  *   If no entry found to delete, an error code of -ENOENT could be returned.
480  */
481 int
482 rte_member_delete(const struct rte_member_setsum *setsum, const void *key,
483                         member_set_t set_id);
484
485 #ifdef __cplusplus
486 }
487 #endif
488
489 #endif /* _RTE_MEMBER_H_ */