xref: /dpdk/lib/member/rte_member.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2017 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson /**
699a2dd95SBruce Richardson  * @file
799a2dd95SBruce Richardson  *
899a2dd95SBruce Richardson  * RTE Membership Library
999a2dd95SBruce Richardson  *
1099a2dd95SBruce Richardson  * The Membership Library is an extension and generalization of a traditional
1199a2dd95SBruce Richardson  * filter (for example Bloom Filter and cuckoo filter) structure that has
1299a2dd95SBruce Richardson  * multiple usages in a variety of workloads and applications. The library is
1399a2dd95SBruce Richardson  * used to test if a key belongs to certain sets. Two types of such
1499a2dd95SBruce Richardson  * "set-summary" structures are implemented: hash-table based (HT) and vector
1599a2dd95SBruce Richardson  * bloom filter (vBF). For HT setsummary, two subtypes or modes are available,
1699a2dd95SBruce Richardson  * cache and non-cache modes. The table below summarize some properties of
1799a2dd95SBruce Richardson  * the different implementations.
1899a2dd95SBruce Richardson  */
1999a2dd95SBruce Richardson 
2099a2dd95SBruce Richardson /**
2199a2dd95SBruce Richardson  * <!--
2299a2dd95SBruce Richardson  * +==========+=====================+================+=========================+
2399a2dd95SBruce Richardson  * |   type   |      vbf            |     HT-cache   |     HT-non-cache        |
2499a2dd95SBruce Richardson  * +==========+=====================+==========================================+
2599a2dd95SBruce Richardson  * |structure |  bloom-filter array |  hash-table like without storing key     |
2699a2dd95SBruce Richardson  * +----------+---------------------+------------------------------------------+
2799a2dd95SBruce Richardson  * |set id    | limited by bf count |           [1, 0x7fff]                    |
2899a2dd95SBruce Richardson  * |          | up to 32.           |                                          |
2999a2dd95SBruce Richardson  * +----------+---------------------+------------------------------------------+
3099a2dd95SBruce Richardson  * |usages &  | small set range,    | can delete,    | cache most recent keys, |
3199a2dd95SBruce Richardson  * |properties| user-specified      | big set range, | have both false-positive|
3299a2dd95SBruce Richardson  * |          | false-positive rate,| small false    | and false-negative      |
3399a2dd95SBruce Richardson  * |          | no deletion support.| positive depend| depend on table size,   |
3499a2dd95SBruce Richardson  * |          |                     | on table size, | automatic overwritten.  |
3599a2dd95SBruce Richardson  * |          |                     | new key does   |                         |
3699a2dd95SBruce Richardson  * |          |                     | not overwrite  |                         |
3799a2dd95SBruce Richardson  * |          |                     | existing key.  |                         |
3899a2dd95SBruce Richardson  * +----------+---------------------+----------------+-------------------------+
39db354bd2SLeyi Rong  * +==========+=============================+
40db354bd2SLeyi Rong  * |   type   |      sketch                 |
41db354bd2SLeyi Rong  * +==========+=============================+
42db354bd2SLeyi Rong  * |structure | counting bloom filter array |
43db354bd2SLeyi Rong  * +----------+-----------------------------+
44db354bd2SLeyi Rong  * |set id    | 1: heavy set, 0: light set  |
45db354bd2SLeyi Rong  * |          |                             |
46db354bd2SLeyi Rong  * +----------+-----------------------------+
47db354bd2SLeyi Rong  * |usages &  | count size of a flow,       |
48db354bd2SLeyi Rong  * |properties| used for heavy hitter       |
49db354bd2SLeyi Rong  * |          | detection.                  |
50db354bd2SLeyi Rong  * +----------+-----------------------------+
5199a2dd95SBruce Richardson  * -->
5299a2dd95SBruce Richardson  */
5399a2dd95SBruce Richardson 
5499a2dd95SBruce Richardson #ifndef _RTE_MEMBER_H_
5599a2dd95SBruce Richardson #define _RTE_MEMBER_H_
5699a2dd95SBruce Richardson 
5799a2dd95SBruce Richardson #include <stdint.h>
58db354bd2SLeyi Rong #include <stdbool.h>
59db354bd2SLeyi Rong #include <inttypes.h>
6099a2dd95SBruce Richardson 
6199a2dd95SBruce Richardson #include <rte_common.h>
6299a2dd95SBruce Richardson 
6399a2dd95SBruce Richardson /** The set ID type that stored internally in hash table based set summary. */
6499a2dd95SBruce Richardson typedef uint16_t member_set_t;
6599a2dd95SBruce Richardson /** Invalid set ID used to mean no match found. */
6699a2dd95SBruce Richardson #define RTE_MEMBER_NO_MATCH 0
6799a2dd95SBruce Richardson /** Maximum size of hash table that can be created. */
6899a2dd95SBruce Richardson #define RTE_MEMBER_ENTRIES_MAX (1 << 30)
6999a2dd95SBruce Richardson /** Maximum number of keys that can be searched as a bulk */
7099a2dd95SBruce Richardson #define RTE_MEMBER_LOOKUP_BULK_MAX 64
7199a2dd95SBruce Richardson /** Entry count per bucket in hash table based mode. */
7299a2dd95SBruce Richardson #define RTE_MEMBER_BUCKET_ENTRIES 16
7399a2dd95SBruce Richardson /** Maximum number of characters in setsum name. */
7499a2dd95SBruce Richardson #define RTE_MEMBER_NAMESIZE 32
75db354bd2SLeyi Rong /** Max value of the random number */
76db354bd2SLeyi Rong #define RTE_RAND_MAX      ~0LLU
77db354bd2SLeyi Rong /**
78db354bd2SLeyi Rong  * As packets skipped in the sampling-based algorithm, the accounting
79db354bd2SLeyi Rong  * results accuracy is not guaranteed in the start stage. There should
80db354bd2SLeyi Rong  * be a "convergence time" to achieve the accuracy after receiving enough
81db354bd2SLeyi Rong  * packets.
82db354bd2SLeyi Rong  * For sketch, use the flag if prefer always bounded mode, which only
83db354bd2SLeyi Rong  * starts sampling after receiving enough packets to keep the results
84db354bd2SLeyi Rong  * accuracy always bounded.
85db354bd2SLeyi Rong  */
86db354bd2SLeyi Rong #define RTE_MEMBER_SKETCH_ALWAYS_BOUNDED 0x01
87db354bd2SLeyi Rong /** For sketch, use the flag if to count packet size instead of packet count */
88db354bd2SLeyi Rong #define RTE_MEMBER_SKETCH_COUNT_BYTE 0x02
8999a2dd95SBruce Richardson 
9099a2dd95SBruce Richardson /** @internal Hash function used by membership library. */
9199a2dd95SBruce Richardson #if defined(RTE_ARCH_X86) || defined(__ARM_FEATURE_CRC32)
9299a2dd95SBruce Richardson #include <rte_hash_crc.h>
9399a2dd95SBruce Richardson #define MEMBER_HASH_FUNC       rte_hash_crc
9499a2dd95SBruce Richardson #else
9599a2dd95SBruce Richardson #include <rte_jhash.h>
9699a2dd95SBruce Richardson #define MEMBER_HASH_FUNC       rte_jhash
9799a2dd95SBruce Richardson #endif
9899a2dd95SBruce Richardson 
99*719834a6SMattias Rönnblom #ifdef __cplusplus
100*719834a6SMattias Rönnblom extern "C" {
101*719834a6SMattias Rönnblom #endif
102*719834a6SMattias Rönnblom 
10399a2dd95SBruce Richardson /** @internal setsummary structure. */
10499a2dd95SBruce Richardson struct rte_member_setsum;
10599a2dd95SBruce Richardson 
10699a2dd95SBruce Richardson /**
10799a2dd95SBruce Richardson  * Parameter struct used to create set summary
10899a2dd95SBruce Richardson  */
10999a2dd95SBruce Richardson struct rte_member_parameters;
11099a2dd95SBruce Richardson 
11199a2dd95SBruce Richardson /**
11299a2dd95SBruce Richardson  * Define different set summary types
11399a2dd95SBruce Richardson  */
11499a2dd95SBruce Richardson enum rte_member_setsum_type {
11599a2dd95SBruce Richardson 	RTE_MEMBER_TYPE_HT = 0,  /**< Hash table based set summary. */
11699a2dd95SBruce Richardson 	RTE_MEMBER_TYPE_VBF,     /**< Vector of bloom filters. */
117db354bd2SLeyi Rong 	RTE_MEMBER_TYPE_SKETCH,
11899a2dd95SBruce Richardson 	RTE_MEMBER_NUM_TYPE
11999a2dd95SBruce Richardson };
12099a2dd95SBruce Richardson 
12199a2dd95SBruce Richardson /** @internal compare function for different arch. */
12299a2dd95SBruce Richardson enum rte_member_sig_compare_function {
12399a2dd95SBruce Richardson 	RTE_MEMBER_COMPARE_SCALAR = 0,
12499a2dd95SBruce Richardson 	RTE_MEMBER_COMPARE_AVX2,
12599a2dd95SBruce Richardson 	RTE_MEMBER_COMPARE_NUM
12699a2dd95SBruce Richardson };
12799a2dd95SBruce Richardson 
128db354bd2SLeyi Rong /* sketch update function with different implementations. */
129db354bd2SLeyi Rong typedef void (*sketch_update_fn_t)(const struct rte_member_setsum *ss,
130db354bd2SLeyi Rong 				   const void *key,
131db354bd2SLeyi Rong 				   uint32_t count);
132db354bd2SLeyi Rong 
133db354bd2SLeyi Rong /* sketch lookup function with different implementations. */
134db354bd2SLeyi Rong typedef uint64_t (*sketch_lookup_fn_t)(const struct rte_member_setsum *ss,
135db354bd2SLeyi Rong 				       const void *key);
136db354bd2SLeyi Rong 
137db354bd2SLeyi Rong /* sketch delete function with different implementations. */
138db354bd2SLeyi Rong typedef void (*sketch_delete_fn_t)(const struct rte_member_setsum *ss,
139db354bd2SLeyi Rong 				   const void *key);
140db354bd2SLeyi Rong 
14199a2dd95SBruce Richardson /** @internal setsummary structure. */
142c6552d9aSTyler Retzlaff struct __rte_cache_aligned rte_member_setsum {
14399a2dd95SBruce Richardson 	enum rte_member_setsum_type type; /* Type of the set summary. */
14499a2dd95SBruce Richardson 	uint32_t key_len;		/* Length of key. */
14599a2dd95SBruce Richardson 	uint32_t prim_hash_seed;	/* Primary hash function seed. */
14699a2dd95SBruce Richardson 	uint32_t sec_hash_seed;		/* Secondary hash function seed. */
14799a2dd95SBruce Richardson 
14899a2dd95SBruce Richardson 	/* Hash table based. */
14999a2dd95SBruce Richardson 	uint32_t bucket_cnt;		/* Number of buckets. */
15099a2dd95SBruce Richardson 	uint32_t bucket_mask;		/* Bit mask to get bucket index. */
15199a2dd95SBruce Richardson 	/* For runtime selecting AVX, scalar, etc for signature comparison. */
15299a2dd95SBruce Richardson 	enum rte_member_sig_compare_function sig_cmp_fn;
15399a2dd95SBruce Richardson 	uint8_t cache;			/* If it is cache mode for ht based. */
15499a2dd95SBruce Richardson 
15599a2dd95SBruce Richardson 	/* Vector bloom filter. */
15699a2dd95SBruce Richardson 	uint32_t num_set;		/* Number of set (bf) in vbf. */
15799a2dd95SBruce Richardson 	uint32_t bits;			/* Number of bits in each bf. */
15899a2dd95SBruce Richardson 	uint32_t bit_mask;	/* Bit mask to get bit location in bf. */
15999a2dd95SBruce Richardson 	uint32_t num_hashes;	/* Number of hash values to index bf. */
16099a2dd95SBruce Richardson 
161db354bd2SLeyi Rong 	/* Parameters for sketch */
162db354bd2SLeyi Rong 	float error_rate;
163db354bd2SLeyi Rong 	float sample_rate;
164db354bd2SLeyi Rong 	uint32_t num_col;
165db354bd2SLeyi Rong 	uint32_t num_row;
166db354bd2SLeyi Rong 	int always_bounded;
167db354bd2SLeyi Rong 	double converge_thresh;
168db354bd2SLeyi Rong 	uint32_t topk;
169db354bd2SLeyi Rong 	uint32_t count_byte;
170db354bd2SLeyi Rong 	uint64_t *hash_seeds;
171db354bd2SLeyi Rong 	sketch_update_fn_t sketch_update; /* Pointer to the sketch update function */
172db354bd2SLeyi Rong 	sketch_lookup_fn_t sketch_lookup; /* Pointer to the sketch lookup function */
173db354bd2SLeyi Rong 	sketch_delete_fn_t sketch_delete; /* Pointer to the sketch delete function */
174db354bd2SLeyi Rong 
175db354bd2SLeyi Rong 	void *runtime_var;
17699a2dd95SBruce Richardson 	uint32_t mul_shift;  /* vbf internal variable used during bit test. */
17799a2dd95SBruce Richardson 	uint32_t div_shift;  /* vbf internal variable used during bit test. */
17899a2dd95SBruce Richardson 
17999a2dd95SBruce Richardson 	void *table;	/* This is the handler of hash table or vBF array. */
18099a2dd95SBruce Richardson 
18199a2dd95SBruce Richardson 
18299a2dd95SBruce Richardson 	/* Second cache line should start here. */
18399a2dd95SBruce Richardson 	uint32_t socket_id;          /* NUMA Socket ID for memory. */
18499a2dd95SBruce Richardson 	char name[RTE_MEMBER_NAMESIZE]; /* Name of this set summary. */
185db354bd2SLeyi Rong #ifdef RTE_ARCH_X86
186db354bd2SLeyi Rong 	bool use_avx512;
187db354bd2SLeyi Rong #endif
188c6552d9aSTyler Retzlaff };
18999a2dd95SBruce Richardson 
19099a2dd95SBruce Richardson /**
19199a2dd95SBruce Richardson  * Parameters used when create the set summary table. Currently user can
19299a2dd95SBruce Richardson  * specify two types of setsummary: HT based and vBF. For HT based, user can
19399a2dd95SBruce Richardson  * specify cache or non-cache mode. Here is a table to describe some differences
19499a2dd95SBruce Richardson  */
195c6552d9aSTyler Retzlaff struct __rte_cache_aligned rte_member_parameters {
19699a2dd95SBruce Richardson 	const char *name;			/**< Name of the hash. */
19799a2dd95SBruce Richardson 
19899a2dd95SBruce Richardson 	/**
19999a2dd95SBruce Richardson 	 * User to specify the type of the setsummary from one of
20099a2dd95SBruce Richardson 	 * rte_member_setsum_type.
20199a2dd95SBruce Richardson 	 *
20299a2dd95SBruce Richardson 	 * HT based setsummary is implemented like a hash table. User should use
20399a2dd95SBruce Richardson 	 * this type when there are many sets.
20499a2dd95SBruce Richardson 	 *
20599a2dd95SBruce Richardson 	 * vBF setsummary is a vector of bloom filters. It is used when number
20699a2dd95SBruce Richardson 	 * of sets is not big (less than 32 for current implementation).
20799a2dd95SBruce Richardson 	 */
20899a2dd95SBruce Richardson 	enum rte_member_setsum_type type;
20999a2dd95SBruce Richardson 
21099a2dd95SBruce Richardson 	/**
21199a2dd95SBruce Richardson 	 * is_cache is only used for HT based setsummary.
21299a2dd95SBruce Richardson 	 *
21399a2dd95SBruce Richardson 	 * If it is HT based setsummary, user to specify the subtype or mode
21499a2dd95SBruce Richardson 	 * of the setsummary. It could be cache, or non-cache mode.
21599a2dd95SBruce Richardson 	 * Set is_cache to be 1 if to use as cache mode.
21699a2dd95SBruce Richardson 	 *
21799a2dd95SBruce Richardson 	 * For cache mode, keys can be evicted out of the HT setsummary. Keys
21899a2dd95SBruce Richardson 	 * with the same signature and map to the same bucket
21999a2dd95SBruce Richardson 	 * will overwrite each other in the setsummary table.
22099a2dd95SBruce Richardson 	 * This mode is useful for the case that the set-summary only
22199a2dd95SBruce Richardson 	 * needs to keep record of the recently inserted keys. Both
22299a2dd95SBruce Richardson 	 * false-negative and false-positive could happen.
22399a2dd95SBruce Richardson 	 *
22499a2dd95SBruce Richardson 	 * For non-cache mode, keys cannot be evicted out of the cache. So for
22599a2dd95SBruce Richardson 	 * this mode the setsummary will become full eventually. Keys with the
22699a2dd95SBruce Richardson 	 * same signature but map to the same bucket will still occupy multiple
22799a2dd95SBruce Richardson 	 * entries. This mode does not give false-negative result.
22899a2dd95SBruce Richardson 	 */
22999a2dd95SBruce Richardson 	uint8_t is_cache;
23099a2dd95SBruce Richardson 
23199a2dd95SBruce Richardson 	/**
23299a2dd95SBruce Richardson 	 * For HT setsummary, num_keys equals to the number of entries of the
23399a2dd95SBruce Richardson 	 * table. When the number of keys inserted in the HT setsummary
23499a2dd95SBruce Richardson 	 * approaches this number, eviction could happen. For cache mode,
23599a2dd95SBruce Richardson 	 * keys could be evicted out of the table. For non-cache mode, keys will
23699a2dd95SBruce Richardson 	 * be evicted to other buckets like cuckoo hash. The table will also
23799a2dd95SBruce Richardson 	 * likely to become full before the number of inserted keys equal to the
23899a2dd95SBruce Richardson 	 * total number of entries.
23999a2dd95SBruce Richardson 	 *
24099a2dd95SBruce Richardson 	 * For vBF, num_keys equal to the expected number of keys that will
24199a2dd95SBruce Richardson 	 * be inserted into the vBF. The implementation assumes the keys are
24299a2dd95SBruce Richardson 	 * evenly distributed to each BF in vBF. This is used to calculate the
24399a2dd95SBruce Richardson 	 * number of bits we need for each BF. User does not specify the size of
24499a2dd95SBruce Richardson 	 * each BF directly because the optimal size depends on the num_keys
24599a2dd95SBruce Richardson 	 * and false positive rate.
24699a2dd95SBruce Richardson 	 */
24799a2dd95SBruce Richardson 	uint32_t num_keys;
24899a2dd95SBruce Richardson 
24999a2dd95SBruce Richardson 	/**
25099a2dd95SBruce Richardson 	 * The length of key is used for hash calculation. Since key is not
25199a2dd95SBruce Richardson 	 * stored in set-summary, large key does not require more memory space.
25299a2dd95SBruce Richardson 	 */
25399a2dd95SBruce Richardson 	uint32_t key_len;
25499a2dd95SBruce Richardson 
25599a2dd95SBruce Richardson 	/**
25699a2dd95SBruce Richardson 	 * num_set is only used for vBF, but not used for HT setsummary.
25799a2dd95SBruce Richardson 	 *
25899a2dd95SBruce Richardson 	 * num_set is equal to the number of BFs in vBF. For current
25999a2dd95SBruce Richardson 	 * implementation, it only supports 1,2,4,8,16,32 BFs in one vBF set
26099a2dd95SBruce Richardson 	 * summary. If other number of sets are needed, for example 5, the user
26199a2dd95SBruce Richardson 	 * should allocate the minimum available value that larger than 5,
26299a2dd95SBruce Richardson 	 * which is 8.
26399a2dd95SBruce Richardson 	 */
26499a2dd95SBruce Richardson 	uint32_t num_set;
26599a2dd95SBruce Richardson 
26699a2dd95SBruce Richardson 	/**
26799a2dd95SBruce Richardson 	 * false_positive_rate is only used for vBF, but not used for HT
26899a2dd95SBruce Richardson 	 * setsummary.
26999a2dd95SBruce Richardson 	 *
27099a2dd95SBruce Richardson 	 * For vBF, false_positive_rate is the user-defined false positive rate
27199a2dd95SBruce Richardson 	 * given expected number of inserted keys (num_keys). It is used to
27299a2dd95SBruce Richardson 	 * calculate the total number of bits for each BF, and the number of
27399a2dd95SBruce Richardson 	 * hash values used during lookup and insertion. For details please
27499a2dd95SBruce Richardson 	 * refer to vBF implementation and membership library documentation.
27599a2dd95SBruce Richardson 	 *
27699a2dd95SBruce Richardson 	 * For HT, This parameter is not directly set by users.
27799a2dd95SBruce Richardson 	 * HT setsummary's false positive rate is in the order of:
27899a2dd95SBruce Richardson 	 * false_pos = (1/bucket_count)*(1/2^16), since we use 16-bit signature.
27999a2dd95SBruce Richardson 	 * This is because two keys needs to map to same bucket and same
28099a2dd95SBruce Richardson 	 * signature to have a collision (false positive). bucket_count is equal
28199a2dd95SBruce Richardson 	 * to number of entries (num_keys) divided by entry count per bucket
28299a2dd95SBruce Richardson 	 * (RTE_MEMBER_BUCKET_ENTRIES). Thus, the false_positive_rate is not
28399a2dd95SBruce Richardson 	 * directly set by users for HT mode.
28499a2dd95SBruce Richardson 	 */
28599a2dd95SBruce Richardson 	float false_positive_rate;
28699a2dd95SBruce Richardson 
28799a2dd95SBruce Richardson 	/**
28899a2dd95SBruce Richardson 	 * We use two seeds to calculate two independent hashes for each key.
28999a2dd95SBruce Richardson 	 *
29099a2dd95SBruce Richardson 	 * For HT type, one hash is used as signature, and the other is used
29199a2dd95SBruce Richardson 	 * for bucket location.
29299a2dd95SBruce Richardson 	 * For vBF type, these two hashes and their combinations are used as
29399a2dd95SBruce Richardson 	 * hash locations to index the bit array.
294ed9e8206SDmitry Kozlyuk 	 * For Sketch type, these seeds are not used.
29599a2dd95SBruce Richardson 	 */
29699a2dd95SBruce Richardson 	uint32_t prim_hash_seed;
29799a2dd95SBruce Richardson 
29899a2dd95SBruce Richardson 	/**
29999a2dd95SBruce Richardson 	 * The secondary seed should be a different value from the primary seed.
30099a2dd95SBruce Richardson 	 */
30199a2dd95SBruce Richardson 	uint32_t sec_hash_seed;
30299a2dd95SBruce Richardson 
303db354bd2SLeyi Rong 	/**
304db354bd2SLeyi Rong 	 * For count(min) sketch data structure, error rate defines the accuracy
305db354bd2SLeyi Rong 	 * required by the user. Higher accuracy leads to more memory usage, but
306db354bd2SLeyi Rong 	 * the flow size is estimated more accurately.
307db354bd2SLeyi Rong 	 */
308db354bd2SLeyi Rong 	float error_rate;
309db354bd2SLeyi Rong 
310db354bd2SLeyi Rong 	/**
311db354bd2SLeyi Rong 	 * Sampling rate means the internal sample rate of the rows of the count
312db354bd2SLeyi Rong 	 * min sketches. Lower sampling rate can reduce CPU overhead, but the
313db354bd2SLeyi Rong 	 * data structure will require more time to converge statistically.
314db354bd2SLeyi Rong 	 */
315db354bd2SLeyi Rong 	float sample_rate;
316db354bd2SLeyi Rong 
317db354bd2SLeyi Rong 	/**
318db354bd2SLeyi Rong 	 * How many top heavy hitter to be reported. The library will internally
319db354bd2SLeyi Rong 	 * keep the keys of heavy hitters for final report.
320db354bd2SLeyi Rong 	 */
321db354bd2SLeyi Rong 	uint32_t top_k;
322db354bd2SLeyi Rong 
323db354bd2SLeyi Rong 	/**
324db354bd2SLeyi Rong 	 * Extra flags that may passed in by user
325db354bd2SLeyi Rong 	 */
326db354bd2SLeyi Rong 	uint32_t extra_flag;
327db354bd2SLeyi Rong 
32899a2dd95SBruce Richardson 	int socket_id;			/**< NUMA Socket ID for memory. */
329c6552d9aSTyler Retzlaff };
33099a2dd95SBruce Richardson 
33199a2dd95SBruce Richardson /**
33299a2dd95SBruce Richardson  * Find an existing set-summary and return a pointer to it.
33399a2dd95SBruce Richardson  *
33499a2dd95SBruce Richardson  * @param name
33599a2dd95SBruce Richardson  *   Name of the set-summary.
33699a2dd95SBruce Richardson  * @return
33799a2dd95SBruce Richardson  *   Pointer to the set-summary or NULL if object not found
33899a2dd95SBruce Richardson  *   with rte_errno set appropriately. Possible rte_errno values include:
33999a2dd95SBruce Richardson  *    - ENOENT - value not available for return
34099a2dd95SBruce Richardson  */
34199a2dd95SBruce Richardson struct rte_member_setsum *
34299a2dd95SBruce Richardson rte_member_find_existing(const char *name);
34399a2dd95SBruce Richardson 
34499a2dd95SBruce Richardson /**
34599a2dd95SBruce Richardson  * Create set-summary (SS).
34699a2dd95SBruce Richardson  *
34799a2dd95SBruce Richardson  * @param params
34899a2dd95SBruce Richardson  *   Parameters to initialize the setsummary.
34999a2dd95SBruce Richardson  * @return
35099a2dd95SBruce Richardson  *   Return the pointer to the setsummary.
35199a2dd95SBruce Richardson  *   Return value is NULL if the creation failed.
35299a2dd95SBruce Richardson  */
35399a2dd95SBruce Richardson struct rte_member_setsum *
35499a2dd95SBruce Richardson rte_member_create(const struct rte_member_parameters *params);
35599a2dd95SBruce Richardson 
35699a2dd95SBruce Richardson /**
35799a2dd95SBruce Richardson  * Lookup key in set-summary (SS).
35899a2dd95SBruce Richardson  * Single key lookup and return as soon as the first match found
35999a2dd95SBruce Richardson  *
36099a2dd95SBruce Richardson  * @param setsum
36199a2dd95SBruce Richardson  *   Pointer of a setsummary.
36299a2dd95SBruce Richardson  * @param key
36399a2dd95SBruce Richardson  *   Pointer of the key to be looked up.
36499a2dd95SBruce Richardson  * @param set_id
36599a2dd95SBruce Richardson  *   Output the set id matches the key.
36699a2dd95SBruce Richardson  * @return
36799a2dd95SBruce Richardson  *   Return 1 for found a match and 0 for not found a match.
36899a2dd95SBruce Richardson  */
36999a2dd95SBruce Richardson int
37099a2dd95SBruce Richardson rte_member_lookup(const struct rte_member_setsum *setsum, const void *key,
37199a2dd95SBruce Richardson 			member_set_t *set_id);
37299a2dd95SBruce Richardson 
37399a2dd95SBruce Richardson /**
37499a2dd95SBruce Richardson  * Lookup bulk of keys in set-summary (SS).
37599a2dd95SBruce Richardson  * Each key lookup returns as soon as the first match found
37699a2dd95SBruce Richardson  *
37799a2dd95SBruce Richardson  * @param setsum
37899a2dd95SBruce Richardson  *   Pointer of a setsummary.
37999a2dd95SBruce Richardson  * @param keys
38099a2dd95SBruce Richardson  *   Pointer of the bulk of keys to be looked up.
38199a2dd95SBruce Richardson  * @param num_keys
38299a2dd95SBruce Richardson  *   Number of keys that will be lookup.
38399a2dd95SBruce Richardson  * @param set_ids
38499a2dd95SBruce Richardson  *   Output set ids for all the keys to this array.
38599a2dd95SBruce Richardson  *   User should preallocate array that can contain all results, which size is
38699a2dd95SBruce Richardson  *   the num_keys.
38799a2dd95SBruce Richardson  * @return
38899a2dd95SBruce Richardson  *   The number of keys that found a match.
38999a2dd95SBruce Richardson  */
39099a2dd95SBruce Richardson int
39199a2dd95SBruce Richardson rte_member_lookup_bulk(const struct rte_member_setsum *setsum,
39299a2dd95SBruce Richardson 			const void **keys, uint32_t num_keys,
39399a2dd95SBruce Richardson 			member_set_t *set_ids);
39499a2dd95SBruce Richardson 
39599a2dd95SBruce Richardson /**
39699a2dd95SBruce Richardson  * Lookup a key in set-summary (SS) for multiple matches.
39799a2dd95SBruce Richardson  * The key lookup will find all matched entries (multiple match).
39899a2dd95SBruce Richardson  * Note that for cache mode of HT, each key can have at most one match. This is
39999a2dd95SBruce Richardson  * because keys with same signature that maps to same bucket will overwrite
40099a2dd95SBruce Richardson  * each other. So multi-match lookup should be used for vBF and non-cache HT.
40199a2dd95SBruce Richardson  *
40299a2dd95SBruce Richardson  * @param setsum
40399a2dd95SBruce Richardson  *   Pointer of a set-summary.
40499a2dd95SBruce Richardson  * @param key
40599a2dd95SBruce Richardson  *   Pointer of the key that to be looked up.
40699a2dd95SBruce Richardson  * @param max_match_per_key
40799a2dd95SBruce Richardson  *   User specified maximum number of matches for each key. The function returns
40899a2dd95SBruce Richardson  *   as soon as this number of matches found for the key.
40999a2dd95SBruce Richardson  * @param set_id
41099a2dd95SBruce Richardson  *   Output set ids for all the matches of the key. User needs to preallocate
41199a2dd95SBruce Richardson  *   the array that can contain max_match_per_key number of results.
41299a2dd95SBruce Richardson  * @return
41399a2dd95SBruce Richardson  *   The number of matches that found for the key.
41499a2dd95SBruce Richardson  *   For cache mode HT set-summary, the number should be at most 1.
41599a2dd95SBruce Richardson  */
41699a2dd95SBruce Richardson int
41799a2dd95SBruce Richardson rte_member_lookup_multi(const struct rte_member_setsum *setsum,
41899a2dd95SBruce Richardson 		const void *key, uint32_t max_match_per_key,
41999a2dd95SBruce Richardson 		member_set_t *set_id);
42099a2dd95SBruce Richardson 
42199a2dd95SBruce Richardson /**
42299a2dd95SBruce Richardson  * Lookup a bulk of keys in set-summary (SS) for multiple matches each key.
42399a2dd95SBruce Richardson  * Each key lookup will find all matched entries (multiple match).
42499a2dd95SBruce Richardson  * Note that for cache mode HT, each key can have at most one match. So
42599a2dd95SBruce Richardson  * multi-match function is mainly used for vBF and non-cache mode HT.
42699a2dd95SBruce Richardson  *
42799a2dd95SBruce Richardson  * @param setsum
42899a2dd95SBruce Richardson  *   Pointer of a setsummary.
42999a2dd95SBruce Richardson  * @param keys
43099a2dd95SBruce Richardson  *   Pointer of the keys to be looked up.
43199a2dd95SBruce Richardson  * @param num_keys
43299a2dd95SBruce Richardson  *   The number of keys that will be lookup.
43399a2dd95SBruce Richardson  * @param max_match_per_key
43499a2dd95SBruce Richardson  *   The possible maximum number of matches for each key.
43599a2dd95SBruce Richardson  * @param match_count
43699a2dd95SBruce Richardson  *   Output the number of matches for each key in an array.
43799a2dd95SBruce Richardson  * @param set_ids
43899a2dd95SBruce Richardson  *   Return set ids for all the matches of all keys. Users pass in a
43999a2dd95SBruce Richardson  *   preallocated 2D array with first dimension as key index and second
44099a2dd95SBruce Richardson  *   dimension as match index. For example set_ids[bulk_size][max_match_per_key]
44199a2dd95SBruce Richardson  * @return
44299a2dd95SBruce Richardson  *   The number of keys that found one or more matches in the set-summary.
44399a2dd95SBruce Richardson  */
44499a2dd95SBruce Richardson int
44599a2dd95SBruce Richardson rte_member_lookup_multi_bulk(const struct rte_member_setsum *setsum,
44699a2dd95SBruce Richardson 		const void **keys, uint32_t num_keys,
44799a2dd95SBruce Richardson 		uint32_t max_match_per_key,
44899a2dd95SBruce Richardson 		uint32_t *match_count,
44999a2dd95SBruce Richardson 		member_set_t *set_ids);
45099a2dd95SBruce Richardson 
45199a2dd95SBruce Richardson /**
45299a2dd95SBruce Richardson  * Insert key into set-summary (SS).
45399a2dd95SBruce Richardson  *
45499a2dd95SBruce Richardson  * @param setsum
45599a2dd95SBruce Richardson  *   Pointer of a set-summary.
45699a2dd95SBruce Richardson  * @param key
45799a2dd95SBruce Richardson  *   Pointer of the key to be added.
45899a2dd95SBruce Richardson  * @param set_id
45999a2dd95SBruce Richardson  *   The set id associated with the key that needs to be added. Different mode
46099a2dd95SBruce Richardson  *   supports different set_id ranges. 0 cannot be used as set_id since
46199a2dd95SBruce Richardson  *   RTE_MEMBER_NO_MATCH by default is set as 0.
46299a2dd95SBruce Richardson  *   For HT mode, the set_id has range as [1, 0x7FFF], MSB is reserved.
46399a2dd95SBruce Richardson  *   For vBF mode the set id is limited by the num_set parameter when create
464db354bd2SLeyi Rong  *   the set-summary. For sketch mode, this id is ignored.
46599a2dd95SBruce Richardson  * @return
46699a2dd95SBruce Richardson  *   HT (cache mode) and vBF should never fail unless the set_id is not in the
46799a2dd95SBruce Richardson  *   valid range. In such case -EINVAL is returned.
46899a2dd95SBruce Richardson  *   For HT (non-cache mode) it could fail with -ENOSPC error code when table is
46999a2dd95SBruce Richardson  *   full.
47099a2dd95SBruce Richardson  *   For success it returns different values for different modes to provide
47199a2dd95SBruce Richardson  *   extra information for users.
47299a2dd95SBruce Richardson  *   Return 0 for HT (cache mode) if the add does not cause
47399a2dd95SBruce Richardson  *   eviction, return 1 otherwise. Return 0 for non-cache mode if success,
47499a2dd95SBruce Richardson  *   -ENOSPC for full, and 1 if cuckoo eviction happens.
475db354bd2SLeyi Rong  *   Always returns 0 for vBF mode and sketch.
47699a2dd95SBruce Richardson  */
47799a2dd95SBruce Richardson int
47899a2dd95SBruce Richardson rte_member_add(const struct rte_member_setsum *setsum, const void *key,
47999a2dd95SBruce Richardson 			member_set_t set_id);
48099a2dd95SBruce Richardson 
48199a2dd95SBruce Richardson /**
482db354bd2SLeyi Rong  * Add the packet byte size into the sketch.
483db354bd2SLeyi Rong  *
484db354bd2SLeyi Rong  * @param setsum
485db354bd2SLeyi Rong  *   Pointer of a set-summary.
486db354bd2SLeyi Rong  * @param key
487db354bd2SLeyi Rong  *   Pointer of the key to be added.
488db354bd2SLeyi Rong  * @param byte_count
489db354bd2SLeyi Rong  *   Add the byte count of the packet into the sketch.
490db354bd2SLeyi Rong  * @return
491db354bd2SLeyi Rong  * Return -EINVAL for invalid parameters, otherwise return 0.
492db354bd2SLeyi Rong  */
493db354bd2SLeyi Rong int
494db354bd2SLeyi Rong rte_member_add_byte_count(const struct rte_member_setsum *setsum,
495db354bd2SLeyi Rong 			  const void *key, uint32_t byte_count);
496db354bd2SLeyi Rong 
497db354bd2SLeyi Rong /**
498db354bd2SLeyi Rong  * Query packet count for a certain flow-key.
499db354bd2SLeyi Rong  *
500db354bd2SLeyi Rong  * @param setsum
501db354bd2SLeyi Rong  *   Pointer of a set-summary.
502db354bd2SLeyi Rong  * @param key
503db354bd2SLeyi Rong  *   Pointer of the key to be added.
504db354bd2SLeyi Rong  * @param count
505db354bd2SLeyi Rong  *   The output packet count or byte count.
506db354bd2SLeyi Rong  * @return
507db354bd2SLeyi Rong  *   Return -EINVAL for invalid parameters.
508db354bd2SLeyi Rong  */
509db354bd2SLeyi Rong int
510db354bd2SLeyi Rong rte_member_query_count(const struct rte_member_setsum *setsum,
511db354bd2SLeyi Rong 		       const void *key, uint64_t *count);
512db354bd2SLeyi Rong 
513db354bd2SLeyi Rong 
514db354bd2SLeyi Rong /**
515db354bd2SLeyi Rong  * Report heavyhitter flow-keys into set-summary (SS).
516db354bd2SLeyi Rong  *
517db354bd2SLeyi Rong  * @param setsum
518db354bd2SLeyi Rong  *   Pointer of a set-summary.
519db354bd2SLeyi Rong  * @param keys
520db354bd2SLeyi Rong  *   Pointer of the output top-k key array.
521db354bd2SLeyi Rong  * @param counts
522db354bd2SLeyi Rong  *   Pointer of the output packet count or byte count array of the top-k keys.
523db354bd2SLeyi Rong  * @return
524db354bd2SLeyi Rong  *   Return -EINVAL for invalid parameters. Return a positive integer indicate
525db354bd2SLeyi Rong  *   how many heavy hitters are reported.
526db354bd2SLeyi Rong  */
527db354bd2SLeyi Rong int
528db354bd2SLeyi Rong rte_member_report_heavyhitter(const struct rte_member_setsum *setsum,
529db354bd2SLeyi Rong 			      void **keys, uint64_t *counts);
530db354bd2SLeyi Rong 
531db354bd2SLeyi Rong 
532db354bd2SLeyi Rong /**
53399a2dd95SBruce Richardson  * De-allocate memory used by set-summary.
53499a2dd95SBruce Richardson  *
53599a2dd95SBruce Richardson  * @param setsum
53699a2dd95SBruce Richardson  *   Pointer to the set summary.
537448e01f1SStephen Hemminger  *   If setsum is NULL, no operation is performed.
53899a2dd95SBruce Richardson  */
53999a2dd95SBruce Richardson void
54099a2dd95SBruce Richardson rte_member_free(struct rte_member_setsum *setsum);
54199a2dd95SBruce Richardson 
54299a2dd95SBruce Richardson /**
54399a2dd95SBruce Richardson  * Reset the set-summary tables. E.g. reset bits to be 0 in BF,
54499a2dd95SBruce Richardson  * reset set_id in each entry to be RTE_MEMBER_NO_MATCH in HT based SS.
54599a2dd95SBruce Richardson  *
54699a2dd95SBruce Richardson  * @param setsum
54799a2dd95SBruce Richardson  *   Pointer to the set-summary.
54899a2dd95SBruce Richardson  */
54999a2dd95SBruce Richardson void
55099a2dd95SBruce Richardson rte_member_reset(const struct rte_member_setsum *setsum);
55199a2dd95SBruce Richardson 
55299a2dd95SBruce Richardson /**
55399a2dd95SBruce Richardson  * Delete items from the set-summary. Note that vBF does not support deletion
55499a2dd95SBruce Richardson  * in current implementation. For vBF, error code of -EINVAL will be returned.
55599a2dd95SBruce Richardson  *
55699a2dd95SBruce Richardson  * @param setsum
55799a2dd95SBruce Richardson  *   Pointer to the set-summary.
55899a2dd95SBruce Richardson  * @param key
55999a2dd95SBruce Richardson  *   Pointer of the key to be deleted.
56099a2dd95SBruce Richardson  * @param set_id
56199a2dd95SBruce Richardson  *   For HT mode, we need both key and its corresponding set_id to
56299a2dd95SBruce Richardson  *   properly delete the key. Without set_id, we may delete other keys with the
56399a2dd95SBruce Richardson  *   same signature.
56499a2dd95SBruce Richardson  * @return
56599a2dd95SBruce Richardson  *   If no entry found to delete, an error code of -ENOENT could be returned.
56699a2dd95SBruce Richardson  */
56799a2dd95SBruce Richardson int
56899a2dd95SBruce Richardson rte_member_delete(const struct rte_member_setsum *setsum, const void *key,
56999a2dd95SBruce Richardson 			member_set_t set_id);
57099a2dd95SBruce Richardson 
57199a2dd95SBruce Richardson #ifdef __cplusplus
57299a2dd95SBruce Richardson }
57399a2dd95SBruce Richardson #endif
57499a2dd95SBruce Richardson 
57599a2dd95SBruce Richardson #endif /* _RTE_MEMBER_H_ */
576