efd: new Elastic Flow Distributor library
[dpdk.git] / lib / librte_efd / rte_efd.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_EFD_H_
35 #define _RTE_EFD_H_
36
37 /**
38  * @file
39  *
40  * RTE EFD Table
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 /*************************************************************************
48  * User selectable constants
49  *************************************************************************/
50
51 /*
52  * If possible, best lookup performance will be achieved by ensuring that
53  * the entire table fits in the L3 cache.
54  *
55  * Some formulas for calculating various sizes are listed below:
56  *
57  * # of chunks =
58  *   2 ^ (ceiling(log2((requested # of rules) /
59  *            (EFD_CHUNK_NUM_GROUPS * EFD_TARGET_GROUP_NUM_RULES))))
60  *
61  * Target # of rules = (# of chunks) * EFD_CHUNK_NUM_GROUPS *
62  *            EFD_TARGET_GROUP_NUM_RULES
63  *
64  * Group Size (in bytes) = 4 (per value bit)
65  *
66  * Table size (in bytes) = RTE_EFD_VALUE_NUM_BITS * (# of chunks) *
67  *            EFD_CHUNK_NUM_GROUPS * (group size)
68  */
69
70 /**
71  * !!! This parameter should be adjusted for your application !!!
72  *
73  * This parameter adjusts the number of bits of value that can be
74  * stored in the table.
75  * For example, setting the number of bits to 3 will allow storing 8 values
76  * in the table (between 0 and 7).
77  *
78  * This number directly affects the performance of both lookups and insertion.
79  * In general, performance decreases as more bits are stored in the table.
80  *
81  * This number is directly proportional to the size of the online region
82  * used for lookups.
83  *
84  * Note that due to the way the CPU operates on memory, best lookup performance
85  * will be achieved when RTE_EFD_VALUE_NUM_BITS is a multiple of 8.
86  * These values align the hash indexes on 16-byte boundaries.
87  * The greatest performance drop is moving from 8->9 bits, 16->17 bits, etc.
88  *
89  * This value must be between 1 and 32
90  */
91 #ifndef RTE_EFD_VALUE_NUM_BITS
92 #define RTE_EFD_VALUE_NUM_BITS (8)
93 #endif
94
95 /*
96  * EFD_TARGET_GROUP_NUM_RULES:
97  *   Adjusts how many groups/chunks are allocated at table creation time
98  *   to support the requested number of rules. Higher values pack entries
99  *   more tightly in memory, resulting in a smaller memory footprint
100  *   for the online table.
101  *   This comes at the cost of lower insert/update performance.
102  *
103  * EFD_MAX_GROUP_NUM_RULES:
104  *   This adjusts the amount of offline memory allocated to store key/value
105  *   pairs for the table. The recommended numbers are upper-bounds for
106  *   this parameter
107  *   - any higher and it becomes very unlikely that a perfect hash function
108  *   can be found for that group size. This value should be at
109  *   least 40% larger than EFD_TARGET_GROUP_NUM_RULES
110  *
111  * Recommended values for various lookuptable and hashfunc sizes are:
112  *
113  *   HASH_FUNC_SIZE = 16, LOOKUPTBL_SIZE = 16:
114  *     EFD_TARGET_GROUP_NUM_RULES = 22
115  *     EFD_MAX_GROUP_NUM_RULES = 28
116  */
117 #define EFD_TARGET_GROUP_NUM_RULES (22)
118 #define EFD_MAX_GROUP_NUM_RULES (28LU)
119
120 #define EFD_MIN_BALANCED_NUM_RULES      5
121
122 /**
123  * Maximum number of keys that can be looked up in one call to efd_lookup_bulk
124  */
125 #ifndef RTE_EFD_BURST_MAX
126 #define RTE_EFD_BURST_MAX (32)
127 #endif
128
129 /** Maximum number of characters in efd name.*/
130 #define RTE_EFD_NAMESIZE                        32
131
132 #if (RTE_EFD_VALUE_NUM_BITS > 0 && RTE_EFD_VALUE_NUM_BITS <= 8)
133 typedef uint8_t efd_value_t;
134 #elif (RTE_EFD_VALUE_NUM_BITS > 8 && RTE_EFD_VALUE_NUM_BITS <= 16)
135 typedef uint16_t efd_value_t;
136 #elif (RTE_EFD_VALUE_NUM_BITS > 16 && RTE_EFD_VALUE_NUM_BITS <= 32)
137 typedef uint32_t efd_value_t;
138 #else
139 #error("RTE_EFD_VALUE_NUM_BITS must be in the range [1:32]")
140 #endif
141
142 #define EFD_LOOKUPTBL_SHIFT (32 - 4)
143 typedef uint16_t efd_lookuptbl_t;
144 typedef uint16_t efd_hashfunc_t;
145
146 /**
147  * Creates an EFD table with a single offline region and multiple per-socket
148  * internally-managed copies of the online table used for lookups
149  *
150  * @param name
151  *   EFD table name
152  * @param max_num_rules
153  *   Minimum number of rules the table should be sized to hold.
154  *   Will be rounded up to the next smallest valid table size
155  * @param key_len
156  *   Length of the key
157  * @param online_cpu_socket_bitmask
158  *   Bitmask specifying which sockets should get a copy of the online table.
159  *   LSB = socket 0, etc.
160  * @param offline_cpu_socket
161  *   Identifies the socket where the offline table will be allocated
162  *   (and most efficiently accessed in the case of updates/insertions)
163  *
164  * @return
165  *   EFD table, or NULL if table allocation failed or the bitmask is invalid
166  */
167 struct rte_efd_table *
168 rte_efd_create(const char *name, uint32_t max_num_rules, uint32_t key_len,
169         uint8_t online_cpu_socket_bitmask, uint8_t offline_cpu_socket);
170
171 /**
172  * Releases the resources from an EFD table
173  *
174  * @param table
175  *   Table to free
176  */
177 void
178 rte_efd_free(struct rte_efd_table *table);
179
180 /**
181  * Find an existing EFD table object and return a pointer to it.
182  *
183  * @param name
184  *   Name of the EFD table as passed to rte_efd_create()
185  * @return
186  *   Pointer to EFD table or NULL if object not found
187  *   with rte_errno set appropriately. Possible rte_errno values include:
188  *    - ENOENT - value not available for return
189  */
190 struct rte_efd_table*
191 rte_efd_find_existing(const char *name);
192
193 #define RTE_EFD_UPDATE_WARN_GROUP_FULL   (1)
194 #define RTE_EFD_UPDATE_NO_CHANGE         (2)
195 #define RTE_EFD_UPDATE_FAILED            (3)
196
197 /**
198  * Computes an updated table entry for the supplied key/value pair.
199  * The update is then immediately applied to the provided table and
200  * all socket-local copies of the chunks are updated.
201  *
202  * @param table
203  *   EFD table to reference
204  * @param socket_id
205  *   Socket ID to use to lookup existing value (ideally caller's socket id)
206  * @param key
207  *   EFD table key to modify
208  * @param value
209  *   Value to associate with the key
210  *
211  * @return
212  *  RTE_EFD_UPDATE_WARN_GROUP_FULL
213  *     Operation is insert, and the last available space in the
214  *     key's group was just used
215  *     Future inserts may fail as groups fill up
216  *     This operation was still successful, and entry contains a valid update
217  *  RTE_EFD_UPDATE_FAILED
218  *     Either the EFD failed to find a suitable perfect hash or the group was full
219  *     This is a fatal error, and the table is now in an indeterminite state
220  *  RTE_EFD_UPDATE_NO_CHANGE
221  *     Operation resulted in no change to the table (same value already exists)
222  *  0 - success
223  */
224 int
225 rte_efd_update(struct rte_efd_table *table, unsigned int socket_id,
226         const void *key, efd_value_t value);
227
228 /**
229  * Removes any value currently associated with the specified key from the table
230  *
231  * @param table
232  *   EFD table to reference
233  * @param socket_id
234  *   Socket ID to use to lookup existing value (ideally caller's socket id)
235  * @param key
236  *   EFD table key to delete
237  * @param prev_value
238  *   If not NULL, will store the previous value here before deleting it
239  *
240  * @return
241  *   0 - successfully found and deleted the key
242  *   nonzero otherwise
243  */
244 int
245 rte_efd_delete(struct rte_efd_table *table, unsigned int socket_id,
246         const void *key, efd_value_t *prev_value);
247
248 /**
249  * Looks up the value associated with a key
250  *
251  * NOTE: Lookups will *always* succeed - this is a property of
252  * using a perfect hash table.
253  * If the specified key was never inserted, a pseudorandom answer will be returned.
254  * There is no way to know based on the lookup if the key was ever inserted
255  * originally, so this must be tracked elsewhere.
256  *
257  * @param table
258  *   EFD table to reference
259  * @param socket_id
260  *   Socket ID to use to lookup existing value (ideally caller's socket id)
261  * @param key
262  *   EFD table key to look up
263  *
264  * @return
265  *   Value associated with the key, or random junk if they key was never inserted
266  */
267 efd_value_t
268 rte_efd_lookup(const struct rte_efd_table *table, unsigned int socket_id,
269                 const void *key);
270
271 /**
272  * Looks up the value associated with several keys.
273  *
274  * NOTE: Lookups will *always* succeed - this is a property of
275  * using a perfect hash table.
276  * If the specified key was never inserted, a pseudorandom answer will be returned.
277  * There is no way to know based on the lookup if the key was ever inserted
278  * originally, so this must be tracked elsewhere.
279  *
280  * @param table
281  *   EFD table to reference
282  * @param socket_id
283  *   Socket ID to use to lookup existing value (ideally caller's socket id)
284  * @param num_keys
285  *   Number of keys in the key_list array, must be less than RTE_EFD_BURST_MAX
286  * @param key_list
287  *   Array of num_keys pointers which point to keys to look up
288  * @param value_list
289  *   Array of size num_keys where lookup values will be stored
290  */
291 void
292 rte_efd_lookup_bulk(const struct rte_efd_table *table, unsigned int socket_id,
293                 int num_keys, const void **key_list,
294                 efd_value_t *value_list);
295
296 #ifdef __cplusplus
297 }
298 #endif
299
300 #endif /* _RTE_EFD_H_ */