Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2016-2017 Intel Corporation
3 : : */
4 : : #include <stdio.h>
5 : : #include <string.h>
6 : : #include <stdint.h>
7 : : #include <stdlib.h>
8 : : #include <inttypes.h>
9 : : #include <errno.h>
10 : : #include <sys/queue.h>
11 : :
12 : : #include <eal_export.h>
13 : : #include <rte_cpuflags.h>
14 : : #include <rte_string_fns.h>
15 : : #include <rte_log.h>
16 : : #include <rte_eal_memconfig.h>
17 : : #include <rte_errno.h>
18 : : #include <rte_malloc.h>
19 : : #include <rte_prefetch.h>
20 : : #include <rte_branch_prediction.h>
21 : : #include <rte_memcpy.h>
22 : : #include <rte_ring.h>
23 : : #include <rte_jhash.h>
24 : : #include <rte_hash_crc.h>
25 : : #include <rte_tailq.h>
26 : :
27 : : #include "rte_efd.h"
28 : : #if defined(RTE_ARCH_X86)
29 : : #elif defined(RTE_ARCH_ARM64)
30 : : #include "rte_efd_arm64.h"
31 : : #endif
32 : :
33 [ - + ]: 252 : RTE_LOG_REGISTER_DEFAULT(efd_logtype, INFO);
34 : : #define RTE_LOGTYPE_EFD efd_logtype
35 : : #define EFD_LOG(level, ...) \
36 : : RTE_LOG_LINE(level, EFD, "" __VA_ARGS__)
37 : :
38 : : #define EFD_KEY(key_idx, table) (table->keys + ((key_idx) * table->key_len))
39 : : /** Hash function used to determine chunk_id and bin_id for a group */
40 : : #define EFD_HASH(key, table) \
41 : : (uint32_t)(rte_jhash(key, table->key_len, 0xbc9f1d34))
42 : : /** Hash function used as constant component of perfect hash search */
43 : : #define EFD_HASHFUNCA(key, table) \
44 : : (uint32_t)(rte_hash_crc(key, table->key_len, 0xbc9f1d35))
45 : : /** Hash function used as multiplicative component of perfect hash search */
46 : : #define EFD_HASHFUNCB(key, table) \
47 : : (uint32_t)(rte_hash_crc(key, table->key_len, 0xbc9f1d36))
48 : :
49 : : /*************************************************************************
50 : : * Fixed constants
51 : : *************************************************************************/
52 : :
53 : : /* These parameters are fixed by the efd_bin_to_group balancing table */
54 : : #define EFD_CHUNK_NUM_GROUPS (64)
55 : : #define EFD_CHUNK_NUM_BINS (256)
56 : : #define EFD_CHUNK_NUM_BIN_TO_GROUP_SETS \
57 : : (EFD_CHUNK_NUM_BINS / EFD_CHUNK_NUM_GROUPS)
58 : :
59 : : /*
60 : : * Target number of rules that each chunk is created to handle.
61 : : * Used when initially allocating the table
62 : : */
63 : : #define EFD_TARGET_CHUNK_NUM_RULES \
64 : : (EFD_CHUNK_NUM_GROUPS * EFD_TARGET_GROUP_NUM_RULES)
65 : : /*
66 : : * Max number of rules that each chunk is created to handle.
67 : : * Used when initially allocating the table
68 : : */
69 : : #define EFD_TARGET_CHUNK_MAX_NUM_RULES \
70 : : (EFD_CHUNK_NUM_GROUPS * EFD_MAX_GROUP_NUM_RULES)
71 : :
72 : : /** This is fixed based on the bin_to_group permutation array */
73 : : #define EFD_MAX_GROUP_NUM_BINS (16)
74 : :
75 : : /**
76 : : * The end of the chunks array needs some extra padding to ensure
77 : : * that vectorization over-reads on the last online chunk stay within
78 : : allocated memory
79 : : */
80 : : #define EFD_NUM_CHUNK_PADDING_BYTES (256)
81 : :
82 : : /* All different internal lookup functions */
83 : : enum efd_lookup_internal_function {
84 : : EFD_LOOKUP_SCALAR = 0,
85 : : EFD_LOOKUP_AVX2,
86 : : EFD_LOOKUP_NEON,
87 : : EFD_LOOKUP_NUM
88 : : };
89 : :
90 : : TAILQ_HEAD(rte_efd_list, rte_tailq_entry);
91 : :
92 : : static struct rte_tailq_elem rte_efd_tailq = {
93 : : .name = "RTE_EFD",
94 : : };
95 [ - + ]: 252 : EAL_REGISTER_TAILQ(rte_efd_tailq);
96 : :
97 : : /** Internal permutation array used to shuffle bins into pseudorandom groups */
98 : : const uint32_t efd_bin_to_group[EFD_CHUNK_NUM_BIN_TO_GROUP_SETS][EFD_CHUNK_NUM_BINS] = {
99 : : {
100 : : 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
101 : : 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
102 : : 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11,
103 : : 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15,
104 : : 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19,
105 : : 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23,
106 : : 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27,
107 : : 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
108 : : 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35,
109 : : 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 39, 39, 39, 39,
110 : : 40, 40, 40, 40, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43,
111 : : 44, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47,
112 : : 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, 51,
113 : : 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55, 55, 55,
114 : : 56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58, 58, 59, 59, 59, 59,
115 : : 60, 60, 60, 60, 61, 61, 61, 61, 62, 62, 62, 62, 63, 63, 63, 63
116 : : },
117 : : {
118 : : 34, 33, 48, 59, 0, 21, 36, 18, 9, 49, 54, 38, 51, 23, 31, 5,
119 : : 44, 23, 37, 52, 11, 4, 58, 20, 38, 40, 38, 22, 26, 28, 42, 6,
120 : : 46, 16, 31, 28, 46, 14, 60, 0, 35, 53, 16, 58, 16, 29, 39, 7,
121 : : 1, 54, 15, 11, 48, 3, 62, 9, 58, 5, 30, 43, 17, 7, 36, 34,
122 : : 6, 36, 2, 14, 10, 1, 47, 47, 20, 45, 62, 56, 34, 25, 39, 18,
123 : : 51, 41, 61, 25, 56, 40, 41, 37, 52, 35, 30, 57, 11, 42, 37, 27,
124 : : 54, 19, 26, 13, 48, 31, 46, 15, 12, 10, 16, 20, 43, 17, 12, 55,
125 : : 45, 18, 8, 41, 7, 31, 42, 63, 12, 14, 21, 57, 24, 40, 5, 41,
126 : : 13, 44, 23, 59, 25, 57, 52, 50, 62, 1, 2, 49, 32, 57, 26, 43,
127 : : 56, 60, 55, 5, 49, 6, 3, 50, 46, 39, 27, 33, 17, 4, 53, 13,
128 : : 2, 19, 36, 51, 63, 0, 22, 33, 59, 28, 29, 23, 45, 33, 53, 27,
129 : : 22, 21, 40, 56, 4, 18, 44, 47, 28, 17, 4, 50, 21, 62, 8, 39,
130 : : 0, 8, 15, 24, 29, 24, 9, 11, 48, 61, 35, 55, 43, 1, 54, 42,
131 : : 53, 60, 22, 3, 32, 52, 25, 8, 15, 60, 7, 55, 27, 63, 19, 10,
132 : : 63, 24, 61, 19, 12, 38, 6, 29, 13, 37, 10, 3, 45, 32, 32, 30,
133 : : 49, 61, 44, 14, 20, 58, 35, 30, 2, 26, 34, 51, 9, 59, 47, 50
134 : : },
135 : : {
136 : : 32, 35, 32, 34, 55, 5, 6, 23, 49, 11, 6, 23, 52, 37, 29, 54,
137 : : 55, 40, 63, 50, 29, 52, 61, 25, 12, 56, 39, 38, 29, 11, 46, 1,
138 : : 40, 11, 19, 56, 7, 28, 51, 16, 15, 48, 21, 51, 60, 31, 14, 22,
139 : : 41, 47, 59, 56, 53, 28, 58, 26, 43, 27, 41, 33, 24, 52, 44, 38,
140 : : 13, 59, 48, 51, 60, 15, 3, 30, 15, 0, 10, 62, 44, 14, 28, 51,
141 : : 38, 2, 41, 26, 25, 49, 10, 12, 55, 57, 27, 35, 19, 33, 0, 30,
142 : : 5, 36, 47, 53, 5, 53, 20, 43, 34, 37, 52, 41, 21, 63, 59, 9,
143 : : 24, 1, 45, 24, 39, 44, 45, 16, 9, 17, 7, 50, 57, 22, 18, 28,
144 : : 25, 45, 2, 40, 58, 15, 17, 3, 1, 27, 61, 39, 19, 0, 19, 21,
145 : : 57, 62, 54, 60, 54, 40, 48, 33, 36, 37, 4, 42, 1, 43, 58, 8,
146 : : 13, 42, 10, 56, 35, 22, 48, 61, 63, 10, 49, 9, 24, 9, 25, 57,
147 : : 33, 18, 13, 31, 42, 36, 36, 55, 30, 37, 53, 34, 59, 4, 4, 23,
148 : : 8, 16, 58, 14, 30, 11, 12, 63, 49, 62, 2, 39, 47, 22, 2, 60,
149 : : 18, 8, 46, 31, 6, 20, 32, 29, 46, 42, 20, 31, 32, 61, 34, 4,
150 : : 47, 26, 20, 43, 26, 21, 7, 3, 16, 35, 18, 44, 27, 62, 13, 23,
151 : : 6, 50, 12, 8, 45, 17, 3, 46, 50, 7, 14, 5, 17, 54, 38, 0
152 : : },
153 : : {
154 : : 29, 56, 5, 7, 54, 48, 23, 37, 35, 44, 52, 40, 33, 49, 60, 0,
155 : : 59, 51, 28, 12, 41, 26, 2, 23, 34, 5, 59, 40, 3, 19, 6, 26,
156 : : 35, 53, 45, 49, 29, 57, 28, 62, 58, 59, 19, 53, 59, 62, 6, 54,
157 : : 13, 15, 48, 50, 45, 21, 41, 12, 34, 40, 24, 56, 19, 21, 35, 18,
158 : : 55, 45, 9, 61, 47, 61, 19, 15, 16, 39, 17, 31, 3, 51, 21, 50,
159 : : 17, 25, 25, 11, 44, 16, 18, 28, 14, 2, 37, 61, 58, 27, 62, 4,
160 : : 14, 17, 1, 9, 46, 28, 37, 0, 53, 43, 57, 7, 57, 46, 21, 41,
161 : : 39, 14, 52, 60, 44, 53, 49, 60, 49, 63, 13, 11, 29, 1, 55, 47,
162 : : 55, 12, 60, 43, 54, 37, 13, 6, 42, 10, 36, 13, 9, 8, 34, 51,
163 : : 31, 32, 12, 7, 57, 2, 26, 14, 3, 30, 63, 3, 32, 1, 5, 11,
164 : : 27, 24, 26, 44, 31, 23, 56, 38, 62, 0, 40, 30, 6, 23, 38, 2,
165 : : 47, 5, 15, 27, 16, 10, 31, 25, 22, 63, 30, 25, 20, 33, 32, 50,
166 : : 29, 43, 55, 10, 50, 45, 56, 20, 4, 7, 27, 46, 11, 16, 22, 52,
167 : : 35, 20, 41, 54, 46, 33, 42, 18, 63, 8, 22, 58, 36, 4, 51, 42,
168 : : 38, 32, 38, 22, 17, 0, 47, 8, 48, 8, 48, 1, 61, 36, 33, 20,
169 : : 24, 39, 39, 18, 30, 36, 9, 43, 42, 24, 10, 58, 4, 15, 34, 52
170 : : },
171 : : };
172 : :
173 : : /*************************************************************************
174 : : * Offline region structures
175 : : *************************************************************************/
176 : :
177 : : /** Online group containing number of rules, values, keys and their bins
178 : : * for EFD_MAX_GROUP_NUM_RULES rules.
179 : : */
180 : : struct efd_offline_group_rules {
181 : : uint32_t num_rules;
182 : : /**< Sum of the number of rules in all bins assigned to this group. */
183 : :
184 : : uint32_t key_idx[EFD_MAX_GROUP_NUM_RULES];
185 : : /**< Array with all keys of the group. */
186 : : efd_value_t value[EFD_MAX_GROUP_NUM_RULES];
187 : : /**< Array with all values of the keys of the group. */
188 : :
189 : : uint8_t bin_id[EFD_MAX_GROUP_NUM_RULES];
190 : : /**< Stores the bin for each corresponding key to
191 : : * avoid having to recompute it
192 : : */
193 : : };
194 : :
195 : : /** Offline chunk record, containing EFD_TARGET_CHUNK_NUM_RULES rules.
196 : : * Those rules are split into EFD_CHUNK_NUM_GROUPS groups per chunk.
197 : : */
198 : : struct efd_offline_chunk_rules {
199 : : uint16_t num_rules;
200 : : /**< Number of rules in the entire chunk;
201 : : * used to detect unbalanced groups
202 : : */
203 : :
204 : : struct efd_offline_group_rules group_rules[EFD_CHUNK_NUM_GROUPS];
205 : : /**< Array of all groups in the chunk. */
206 : : };
207 : :
208 : : /*************************************************************************
209 : : * Online region structures
210 : : *************************************************************************/
211 : :
212 : : /** Online group containing values for EFD_MAX_GROUP_NUM_RULES rules. */
213 : : struct efd_online_group_entry {
214 : : efd_hashfunc_t hash_idx[RTE_EFD_VALUE_NUM_BITS];
215 : : efd_lookuptbl_t lookup_table[RTE_EFD_VALUE_NUM_BITS];
216 : : };
217 : :
218 : : /**
219 : : * A single chunk record, containing EFD_TARGET_CHUNK_NUM_RULES rules.
220 : : * Those rules are split into EFD_CHUNK_NUM_GROUPS groups per chunk.
221 : : */
222 : : struct efd_online_chunk {
223 : : uint8_t bin_choice_list[(EFD_CHUNK_NUM_BINS * 2 + 7) / 8];
224 : : /**< This is a packed indirection index into the 'groups' array.
225 : : * Each byte contains four two-bit values which index into
226 : : * the efd_bin_to_group array.
227 : : * The efd_bin_to_group array returns the index into the groups array
228 : : */
229 : :
230 : : struct efd_online_group_entry groups[EFD_CHUNK_NUM_GROUPS];
231 : : /**< Array of all the groups in the chunk. */
232 : : };
233 : :
234 : : /**
235 : : * EFD table structure
236 : : */
237 : : struct rte_efd_table {
238 : : char name[RTE_EFD_NAMESIZE]; /**< Name of the efd table. */
239 : :
240 : : uint32_t key_len; /**< Length of the key stored offline */
241 : :
242 : : uint32_t max_num_rules;
243 : : /**< Static maximum number of entries the table was constructed to hold. */
244 : :
245 : : uint32_t num_rules;
246 : : /**< Number of entries currently in the table . */
247 : :
248 : : uint32_t num_chunks;
249 : : /**< Number of chunks in the table needed to support num_rules. */
250 : :
251 : : uint32_t num_chunks_shift;
252 : : /**< Bits to shift to get chunk id, instead of dividing by num_chunk. */
253 : :
254 : : enum efd_lookup_internal_function lookup_fn;
255 : : /**< Indicates which lookup function to use. */
256 : :
257 : : struct efd_online_chunk *chunks[RTE_MAX_NUMA_NODES];
258 : : /**< Dynamic array of size num_chunks of chunk records. */
259 : :
260 : : struct efd_offline_chunk_rules *offline_chunks;
261 : : /**< Dynamic array of size num_chunks of key-value pairs. */
262 : :
263 : : struct rte_ring *free_slots;
264 : : /**< Ring that stores all indexes of the free slots in the key table */
265 : :
266 : : uint8_t *keys; /**< Dynamic array of size max_num_rules of keys */
267 : : };
268 : :
269 : : /**
270 : : * Computes the chunk ID for a given key hash
271 : : *
272 : : * @param table
273 : : * EFD table to reference
274 : : * @param hashed_key
275 : : * 32-bit key hash returned by EFD_HASH
276 : : *
277 : : * @return
278 : : * chunk ID containing this key hash
279 : : */
280 : : static inline uint32_t
281 : : efd_get_chunk_id(const struct rte_efd_table * const table,
282 : : const uint32_t hashed_key)
283 : : {
284 : 0 : return hashed_key & (table->num_chunks - 1);
285 : : }
286 : :
287 : : /**
288 : : * Computes the bin ID for a given key hash
289 : : *
290 : : * @param table
291 : : * EFD table to reference
292 : : * @param hashed_key
293 : : * 32-bit key hash returned by EFD_HASH
294 : : *
295 : : * @return bin ID containing this key hash
296 : : */
297 : : static inline uint32_t
298 : : efd_get_bin_id(const struct rte_efd_table * const table,
299 : : const uint32_t hashed_key)
300 : : {
301 : 0 : return (hashed_key >> table->num_chunks_shift) & (EFD_CHUNK_NUM_BINS - 1);
302 : : }
303 : :
304 : : /**
305 : : * Looks up the current permutation choice for a particular bin in the online table
306 : : *
307 : : * @param table
308 : : * EFD table to reference
309 : : * @param socket_id
310 : : * Socket ID to use to look up existing values (ideally caller's socket id)
311 : : * @param chunk_id
312 : : * Chunk ID of bin to look up
313 : : * @param bin_id
314 : : * Bin ID to look up
315 : : *
316 : : * @return
317 : : * Currently active permutation choice in the online table
318 : : */
319 : : static inline uint8_t
320 : : efd_get_choice(const struct rte_efd_table * const table,
321 : : const unsigned int socket_id, const uint32_t chunk_id,
322 : : const uint32_t bin_id)
323 : : {
324 : 0 : struct efd_online_chunk *chunk = &table->chunks[socket_id][chunk_id];
325 : :
326 : : /*
327 : : * Grab the chunk (byte) that contains the choices
328 : : * for four neighboring bins.
329 : : */
330 : 0 : uint8_t choice_chunk =
331 : 0 : chunk->bin_choice_list[bin_id / EFD_CHUNK_NUM_BIN_TO_GROUP_SETS];
332 : :
333 : : /*
334 : : * Compute the offset into the chunk that contains
335 : : * the group_id lookup position
336 : : */
337 : 0 : int offset = (bin_id & 0x3) * 2;
338 : :
339 : : /* Extract from the byte just the desired lookup position */
340 : 0 : return (uint8_t) ((choice_chunk >> offset) & 0x3);
341 : : }
342 : :
343 : : /**
344 : : * Compute the chunk_id and bin_id for a given key
345 : : *
346 : : * @param table
347 : : * EFD table to reference
348 : : * @param key
349 : : * Key to hash and find location of
350 : : * @param chunk_id
351 : : * Computed chunk ID
352 : : * @param bin_id
353 : : * Computed bin ID
354 : : */
355 : : static inline void
356 : 0 : efd_compute_ids(const struct rte_efd_table * const table,
357 : : const void *key, uint32_t * const chunk_id, uint32_t * const bin_id)
358 : : {
359 : : /* Compute the position of the entry in the hash table */
360 : 0 : uint32_t h = EFD_HASH(key, table);
361 : :
362 : : /* Compute the chunk_id where that entry can be found */
363 : 0 : *chunk_id = efd_get_chunk_id(table, h);
364 : :
365 : : /*
366 : : * Compute the bin within that chunk where the entry
367 : : * can be found (0 - 255)
368 : : */
369 : 0 : *bin_id = efd_get_bin_id(table, h);
370 : 0 : }
371 : :
372 : : /**
373 : : * Search for a hash function for a group that satisfies all group results
374 : : */
375 : : static inline int
376 : 0 : efd_search_hash(struct rte_efd_table * const table,
377 : : const struct efd_offline_group_rules * const off_group,
378 : : struct efd_online_group_entry * const on_group)
379 : : {
380 : : efd_hashfunc_t hash_idx;
381 : : efd_hashfunc_t start_hash_idx[RTE_EFD_VALUE_NUM_BITS];
382 : : efd_lookuptbl_t start_lookup_table[RTE_EFD_VALUE_NUM_BITS];
383 : :
384 : : uint32_t i, j, rule_id;
385 : : uint32_t hash_val_a[EFD_MAX_GROUP_NUM_RULES];
386 : : uint32_t hash_val_b[EFD_MAX_GROUP_NUM_RULES];
387 : : uint32_t hash_val[EFD_MAX_GROUP_NUM_RULES];
388 : :
389 : :
390 : 0 : rte_prefetch0(off_group->value);
391 : :
392 : : /*
393 : : * Prepopulate the hash_val tables by running the two hash functions
394 : : * for each provided rule
395 : : */
396 [ # # ]: 0 : for (i = 0; i < off_group->num_rules; i++) {
397 : 0 : void *key_stored = EFD_KEY(off_group->key_idx[i], table);
398 : 0 : hash_val_b[i] = EFD_HASHFUNCB(key_stored, table);
399 : 0 : hash_val_a[i] = EFD_HASHFUNCA(key_stored, table);
400 : : }
401 : :
402 [ # # ]: 0 : for (i = 0; i < RTE_EFD_VALUE_NUM_BITS; i++) {
403 : 0 : hash_idx = on_group->hash_idx[i];
404 : 0 : start_hash_idx[i] = hash_idx;
405 : 0 : start_lookup_table[i] = on_group->lookup_table[i];
406 : :
407 : : do {
408 : : efd_lookuptbl_t lookup_table = 0;
409 : : efd_lookuptbl_t lookup_table_complement = 0;
410 : :
411 [ # # ]: 0 : for (rule_id = 0; rule_id < off_group->num_rules; rule_id++)
412 : 0 : hash_val[rule_id] = hash_val_a[rule_id] + (hash_idx *
413 : 0 : hash_val_b[rule_id]);
414 : :
415 : : /*
416 : : * The goal here is to find a hash function for this
417 : : * particular bit entry that meets the following criteria:
418 : : * The most significant bits of the hash result define a
419 : : * shift into the lookup table where the bit will be stored
420 : : */
421 : :
422 : : /* Iterate over each provided rule */
423 [ # # ]: 0 : for (rule_id = 0; rule_id < off_group->num_rules;
424 : 0 : rule_id++) {
425 : : /*
426 : : * Use the few most significant bits (number based on
427 : : * EFD_LOOKUPTBL_SIZE) to see what position the
428 : : * expected bit should be set in the lookup_table
429 : : */
430 : 0 : uint32_t bucket_idx = hash_val[rule_id] >>
431 : : EFD_LOOKUPTBL_SHIFT;
432 : :
433 : : /*
434 : : * Get the current bit of interest.
435 : : * This only find an appropriate hash function
436 : : * for one bit at a time of the rule
437 : : */
438 : 0 : efd_lookuptbl_t expected =
439 : 0 : (off_group->value[rule_id] >> i) & 0x1;
440 : :
441 : : /*
442 : : * Add the expected bit (if set) to a map
443 : : * (lookup_table). Also set its complement
444 : : * in lookup_table_complement
445 : : */
446 : 0 : lookup_table |= expected << bucket_idx;
447 : 0 : lookup_table_complement |= (1 - expected)
448 : 0 : << bucket_idx;
449 : :
450 : : /*
451 : : * If ever the hash function of two different
452 : : * elements result in different values at the
453 : : * same location in the lookup_table,
454 : : * the current hash_idx is not valid.
455 : : */
456 [ # # ]: 0 : if (lookup_table & lookup_table_complement)
457 : : break;
458 : : }
459 : :
460 : : /*
461 : : * Check if the previous loop completed without
462 : : * breaking early
463 : : */
464 [ # # ]: 0 : if (rule_id == off_group->num_rules) {
465 : : /*
466 : : * Current hash function worked, store it
467 : : * for the current group
468 : : */
469 : 0 : on_group->hash_idx[i] = hash_idx;
470 : 0 : on_group->lookup_table[i] = lookup_table;
471 : :
472 : : /*
473 : : * Make sure that the hash function has changed
474 : : * from the starting value
475 : : */
476 : 0 : hash_idx = start_hash_idx[i] + 1;
477 : 0 : break;
478 : : }
479 : 0 : hash_idx++;
480 : :
481 [ # # ]: 0 : } while (hash_idx != start_hash_idx[i]);
482 : :
483 : : /* Failed to find perfect hash for this group */
484 [ # # ]: 0 : if (hash_idx == start_hash_idx[i]) {
485 : : /*
486 : : * Restore previous hash_idx and lookup_table
487 : : * for all value bits
488 : : */
489 [ # # ]: 0 : for (j = 0; j < i; j++) {
490 : 0 : on_group->hash_idx[j] = start_hash_idx[j];
491 : 0 : on_group->lookup_table[j] = start_lookup_table[j];
492 : : }
493 : : return 1;
494 : : }
495 : : }
496 : :
497 : : return 0;
498 : : }
499 : :
500 : : RTE_EXPORT_SYMBOL(rte_efd_create)
501 : : struct rte_efd_table *
502 : 0 : rte_efd_create(const char *name, uint32_t max_num_rules, uint32_t key_len,
503 : : uint64_t online_cpu_socket_bitmask, uint8_t offline_cpu_socket)
504 : : {
505 : : struct rte_efd_table *table = NULL;
506 : : uint8_t *key_array = NULL;
507 : : uint32_t num_chunks, num_chunks_shift;
508 : : uint8_t socket_id;
509 : : struct rte_efd_list *efd_list = NULL;
510 : : struct rte_tailq_entry *te;
511 : : uint64_t offline_table_size;
512 : : char ring_name[RTE_RING_NAMESIZE];
513 : : struct rte_ring *r = NULL;
514 : : unsigned int i;
515 : :
516 : 0 : efd_list = RTE_TAILQ_CAST(rte_efd_tailq.head, rte_efd_list);
517 : :
518 [ # # ]: 0 : if (online_cpu_socket_bitmask == 0) {
519 : 0 : EFD_LOG(ERR, "At least one CPU socket must be enabled "
520 : : "in the bitmask");
521 : 0 : return NULL;
522 : : }
523 : :
524 [ # # ]: 0 : if (max_num_rules == 0) {
525 : 0 : EFD_LOG(ERR, "Max num rules must be higher than 0");
526 : 0 : return NULL;
527 : : }
528 : :
529 : : /*
530 : : * Compute the minimum number of chunks (smallest power of 2)
531 : : * that can hold all of the rules
532 : : */
533 [ # # ]: 0 : if (max_num_rules % EFD_TARGET_CHUNK_NUM_RULES == 0)
534 : 0 : num_chunks = rte_align32pow2(max_num_rules /
535 : : EFD_TARGET_CHUNK_NUM_RULES);
536 : : else
537 : 0 : num_chunks = rte_align32pow2((max_num_rules /
538 : : EFD_TARGET_CHUNK_NUM_RULES) + 1);
539 : :
540 : : num_chunks_shift = rte_bsf32(num_chunks);
541 : :
542 : 0 : rte_mcfg_tailq_write_lock();
543 : :
544 : : /*
545 : : * Guarantee there's no existing: this is normally already checked
546 : : * by ring creation above
547 : : */
548 [ # # ]: 0 : TAILQ_FOREACH(te, efd_list, next)
549 : : {
550 : 0 : table = (struct rte_efd_table *) te->data;
551 [ # # ]: 0 : if (strncmp(name, table->name, RTE_EFD_NAMESIZE) == 0)
552 : : break;
553 : : }
554 : :
555 : : table = NULL;
556 [ # # ]: 0 : if (te != NULL) {
557 : 0 : rte_errno = EEXIST;
558 : : te = NULL;
559 : 0 : goto error_unlock_exit;
560 : : }
561 : :
562 : 0 : te = rte_zmalloc("EFD_TAILQ_ENTRY", sizeof(*te), 0);
563 [ # # ]: 0 : if (te == NULL) {
564 : 0 : EFD_LOG(ERR, "tailq entry allocation failed");
565 : 0 : goto error_unlock_exit;
566 : : }
567 : :
568 : : /* Create a new EFD table management structure */
569 : 0 : table = rte_zmalloc_socket(NULL,
570 : : sizeof(struct rte_efd_table),
571 : : RTE_CACHE_LINE_SIZE,
572 : : offline_cpu_socket);
573 [ # # ]: 0 : if (table == NULL) {
574 : 0 : EFD_LOG(ERR, "Allocating EFD table management structure"
575 : : " on socket %u failed",
576 : : offline_cpu_socket);
577 : 0 : goto error_unlock_exit;
578 : : }
579 : :
580 : :
581 : 0 : EFD_LOG(DEBUG, "Allocated EFD table management structure "
582 : : "on socket %u", offline_cpu_socket);
583 : :
584 : 0 : table->max_num_rules = num_chunks * EFD_TARGET_CHUNK_MAX_NUM_RULES;
585 : 0 : table->num_rules = 0;
586 : 0 : table->num_chunks = num_chunks;
587 : 0 : table->num_chunks_shift = num_chunks_shift;
588 : 0 : table->key_len = key_len;
589 : :
590 : : /* key_array */
591 : 0 : key_array = rte_zmalloc_socket(NULL,
592 : 0 : table->max_num_rules * table->key_len,
593 : : RTE_CACHE_LINE_SIZE,
594 : : offline_cpu_socket);
595 [ # # ]: 0 : if (key_array == NULL) {
596 : 0 : EFD_LOG(ERR, "Allocating key array"
597 : : " on socket %u failed",
598 : : offline_cpu_socket);
599 : 0 : goto error_unlock_exit;
600 : : }
601 : 0 : table->keys = key_array;
602 : 0 : strlcpy(table->name, name, sizeof(table->name));
603 : :
604 : 0 : EFD_LOG(DEBUG, "Creating an EFD table with %u chunks,"
605 : : " which potentially supports %u entries",
606 : : num_chunks, table->max_num_rules);
607 : :
608 : : /* Make sure all the allocatable table pointers are NULL initially */
609 [ # # ]: 0 : for (socket_id = 0; socket_id < RTE_MAX_NUMA_NODES; socket_id++)
610 : 0 : table->chunks[socket_id] = NULL;
611 : 0 : table->offline_chunks = NULL;
612 : :
613 : : /*
614 : : * Allocate one online table per socket specified
615 : : * in the user-supplied bitmask
616 : : */
617 : 0 : uint64_t online_table_size = num_chunks * sizeof(struct efd_online_chunk) +
618 : : EFD_NUM_CHUNK_PADDING_BYTES;
619 : :
620 [ # # ]: 0 : for (socket_id = 0; socket_id < RTE_MAX_NUMA_NODES; socket_id++) {
621 [ # # ]: 0 : if ((online_cpu_socket_bitmask >> socket_id) & 0x01) {
622 : : /*
623 : : * Allocate all of the EFD table chunks (the online portion)
624 : : * as a continuous block
625 : : */
626 : 0 : table->chunks[socket_id] =
627 : 0 : rte_zmalloc_socket(
628 : : NULL,
629 : : online_table_size,
630 : : RTE_CACHE_LINE_SIZE,
631 : : socket_id);
632 [ # # ]: 0 : if (table->chunks[socket_id] == NULL) {
633 : 0 : EFD_LOG(ERR,
634 : : "Allocating EFD online table on "
635 : : "socket %u failed",
636 : : socket_id);
637 : 0 : goto error_unlock_exit;
638 : : }
639 : 0 : EFD_LOG(DEBUG,
640 : : "Allocated EFD online table of size "
641 : : "%"PRIu64" bytes (%.2f MB) on socket %u",
642 : : online_table_size,
643 : : (float) online_table_size /
644 : : (1024.0F * 1024.0F),
645 : : socket_id);
646 : : }
647 : : }
648 : :
649 : : #if defined(RTE_ARCH_X86)
650 : : /*
651 : : * For less than 4 bits, scalar function performs better
652 : : * than vectorised version
653 : : */
654 [ # # ]: 0 : if (RTE_EFD_VALUE_NUM_BITS > 3
655 : 0 : && rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2)
656 [ # # ]: 0 : && rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_256)
657 : 0 : table->lookup_fn = EFD_LOOKUP_AVX2;
658 : : else
659 : : #endif
660 : : #if defined(RTE_ARCH_ARM64)
661 : : /*
662 : : * For less than or equal to 16 bits, scalar function performs better
663 : : * than vectorised version
664 : : */
665 : : if (RTE_EFD_VALUE_NUM_BITS > 16 &&
666 : : rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON) &&
667 : : rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128)
668 : : table->lookup_fn = EFD_LOOKUP_NEON;
669 : : else
670 : : #endif
671 : 0 : table->lookup_fn = EFD_LOOKUP_SCALAR;
672 : :
673 : : /*
674 : : * Allocate the EFD table offline portion (with the actual rules
675 : : * mapping keys to values) as a continuous block.
676 : : * This could be several gigabytes of memory.
677 : : */
678 : 0 : offline_table_size = num_chunks * sizeof(struct efd_offline_chunk_rules);
679 : 0 : table->offline_chunks =
680 : 0 : rte_zmalloc_socket(NULL,
681 : : offline_table_size,
682 : : RTE_CACHE_LINE_SIZE,
683 : : offline_cpu_socket);
684 [ # # ]: 0 : if (table->offline_chunks == NULL) {
685 : 0 : EFD_LOG(ERR, "Allocating EFD offline table on socket %u "
686 : : "failed", offline_cpu_socket);
687 : 0 : goto error_unlock_exit;
688 : : }
689 : :
690 : 0 : EFD_LOG(DEBUG,
691 : : "Allocated EFD offline table of size %"PRIu64" bytes "
692 : : " (%.2f MB) on socket %u", offline_table_size,
693 : : (float) offline_table_size / (1024.0F * 1024.0F),
694 : : offline_cpu_socket);
695 : :
696 : 0 : te->data = (void *) table;
697 : 0 : TAILQ_INSERT_TAIL(efd_list, te, next);
698 : 0 : rte_mcfg_tailq_write_unlock();
699 : :
700 : : snprintf(ring_name, sizeof(ring_name), "HT_%s", table->name);
701 : : /* Create ring (Dummy slot index is not enqueued) */
702 : 0 : r = rte_ring_create(ring_name, rte_align32pow2(table->max_num_rules),
703 : : offline_cpu_socket, 0);
704 [ # # ]: 0 : if (r == NULL) {
705 : 0 : EFD_LOG(ERR, "memory allocation failed");
706 : 0 : rte_efd_free(table);
707 : 0 : return NULL;
708 : : }
709 : :
710 : : /* Populate free slots ring. Entry zero is reserved for key misses. */
711 [ # # ]: 0 : for (i = 0; i < table->max_num_rules; i++)
712 : 0 : rte_ring_sp_enqueue(r, (void *) ((uintptr_t) i));
713 : :
714 : 0 : table->free_slots = r;
715 : 0 : return table;
716 : :
717 : 0 : error_unlock_exit:
718 : 0 : rte_mcfg_tailq_write_unlock();
719 : 0 : rte_free(te);
720 : 0 : rte_efd_free(table);
721 : :
722 : 0 : return NULL;
723 : : }
724 : :
725 : : RTE_EXPORT_SYMBOL(rte_efd_find_existing)
726 : : struct rte_efd_table *
727 : 0 : rte_efd_find_existing(const char *name)
728 : : {
729 : : struct rte_efd_table *table = NULL;
730 : : struct rte_tailq_entry *te;
731 : : struct rte_efd_list *efd_list;
732 : :
733 : 0 : efd_list = RTE_TAILQ_CAST(rte_efd_tailq.head, rte_efd_list);
734 : :
735 : 0 : rte_mcfg_tailq_read_lock();
736 : :
737 [ # # ]: 0 : TAILQ_FOREACH(te, efd_list, next)
738 : : {
739 : 0 : table = (struct rte_efd_table *) te->data;
740 [ # # ]: 0 : if (strncmp(name, table->name, RTE_EFD_NAMESIZE) == 0)
741 : : break;
742 : : }
743 : 0 : rte_mcfg_tailq_read_unlock();
744 : :
745 [ # # ]: 0 : if (te == NULL) {
746 : 0 : rte_errno = ENOENT;
747 : 0 : return NULL;
748 : : }
749 : : return table;
750 : : }
751 : :
752 : : RTE_EXPORT_SYMBOL(rte_efd_free)
753 : : void
754 : 0 : rte_efd_free(struct rte_efd_table *table)
755 : : {
756 : : uint8_t socket_id;
757 : : struct rte_efd_list *efd_list;
758 : : struct rte_tailq_entry *te, *temp;
759 : :
760 [ # # ]: 0 : if (table == NULL)
761 : : return;
762 : :
763 [ # # ]: 0 : for (socket_id = 0; socket_id < RTE_MAX_NUMA_NODES; socket_id++)
764 : 0 : rte_free(table->chunks[socket_id]);
765 : :
766 : 0 : efd_list = RTE_TAILQ_CAST(rte_efd_tailq.head, rte_efd_list);
767 : 0 : rte_mcfg_tailq_write_lock();
768 : :
769 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(te, efd_list, next, temp) {
770 [ # # ]: 0 : if (te->data == (void *) table) {
771 [ # # ]: 0 : TAILQ_REMOVE(efd_list, te, next);
772 : 0 : rte_free(te);
773 : 0 : break;
774 : : }
775 : : }
776 : :
777 : 0 : rte_mcfg_tailq_write_unlock();
778 : 0 : rte_ring_free(table->free_slots);
779 : 0 : rte_free(table->offline_chunks);
780 : 0 : rte_free(table->keys);
781 : 0 : rte_free(table);
782 : : }
783 : :
784 : : /**
785 : : * Applies a previously computed table entry to the specified table for all
786 : : * socket-local copies of the online table.
787 : : * Intended to apply an update for only a single change
788 : : * to a key/value pair at a time
789 : : *
790 : : * @param table
791 : : * EFD table to reference
792 : : * @param socket_id
793 : : * Socket ID to use to lookup existing values (ideally caller's socket id)
794 : : * @param chunk_id
795 : : * Chunk index to update
796 : : * @param group_id
797 : : * Group index to update
798 : : * @param bin_id
799 : : * Bin within the group that this update affects
800 : : * @param new_bin_choice
801 : : * Newly chosen permutation which this bin should use - only lower 2 bits
802 : : * @param new_group_entry
803 : : * Previously computed updated chunk/group entry
804 : : */
805 : : static inline void
806 : 0 : efd_apply_update(struct rte_efd_table * const table, const unsigned int socket_id,
807 : : const uint32_t chunk_id, const uint32_t group_id,
808 : : const uint32_t bin_id, const uint8_t new_bin_choice,
809 : : const struct efd_online_group_entry * const new_group_entry)
810 : : {
811 : : int i;
812 : 0 : struct efd_online_chunk *chunk = &table->chunks[socket_id][chunk_id];
813 : 0 : uint8_t bin_index = bin_id / EFD_CHUNK_NUM_BIN_TO_GROUP_SETS;
814 : :
815 : : /*
816 : : * Grab the current byte that contains the choices
817 : : * for four neighboring bins
818 : : */
819 : 0 : uint8_t choice_chunk =
820 : 0 : chunk->bin_choice_list[bin_index];
821 : :
822 : :
823 : : /* Compute the offset into the chunk that needs to be updated */
824 : 0 : int offset = (bin_id & 0x3) * 2;
825 : :
826 : : /* Zero the two bits of interest and set them to new_bin_choice */
827 : 0 : choice_chunk = (choice_chunk & (~(0x03 << offset)))
828 : 0 : | ((new_bin_choice & 0x03) << offset);
829 : :
830 : : /* Update the online table with the new data across all sockets */
831 [ # # ]: 0 : for (i = 0; i < RTE_MAX_NUMA_NODES; i++) {
832 [ # # ]: 0 : if (table->chunks[i] != NULL) {
833 : 0 : memcpy(&(table->chunks[i][chunk_id].groups[group_id]),
834 : : new_group_entry,
835 : : sizeof(struct efd_online_group_entry));
836 : 0 : table->chunks[i][chunk_id].bin_choice_list[bin_index] =
837 : : choice_chunk;
838 : : }
839 : : }
840 : 0 : }
841 : :
842 : : /*
843 : : * Move the bin from prev group to the new group
844 : : */
845 : : static inline void
846 : 0 : move_groups(uint32_t bin_id, uint8_t bin_size,
847 : : struct efd_offline_group_rules *new_group,
848 : : struct efd_offline_group_rules * const current_group)
849 : : {
850 : :
851 : : uint8_t empty_idx = 0;
852 : : unsigned int i;
853 : :
854 [ # # ]: 0 : if (new_group == current_group)
855 : : return;
856 : :
857 [ # # ]: 0 : for (i = 0; i < current_group->num_rules; i++) {
858 : : /*
859 : : * Move keys that belong to the same bin
860 : : * to the new group
861 : : */
862 [ # # ]: 0 : if (current_group->bin_id[i] == bin_id) {
863 : 0 : new_group->key_idx[new_group->num_rules] =
864 : 0 : current_group->key_idx[i];
865 : 0 : new_group->value[new_group->num_rules] =
866 : 0 : current_group->value[i];
867 : 0 : new_group->bin_id[new_group->num_rules] =
868 : : current_group->bin_id[i];
869 : 0 : new_group->num_rules++;
870 : : } else {
871 [ # # ]: 0 : if (i != empty_idx) {
872 : : /*
873 : : * Need to move this key towards
874 : : * the top of the array
875 : : */
876 : 0 : current_group->key_idx[empty_idx] =
877 : 0 : current_group->key_idx[i];
878 : 0 : current_group->value[empty_idx] =
879 : 0 : current_group->value[i];
880 : 0 : current_group->bin_id[empty_idx] =
881 : : current_group->bin_id[i];
882 : : }
883 : 0 : empty_idx++;
884 : : }
885 : :
886 : : }
887 : 0 : current_group->num_rules -= bin_size;
888 : : }
889 : :
890 : : /*
891 : : * Revert group/s to their previous state before
892 : : * trying to insert/add a new key
893 : : */
894 : : static inline void
895 : 0 : revert_groups(struct efd_offline_group_rules *previous_group,
896 : : struct efd_offline_group_rules *current_group, uint8_t bin_size)
897 : : {
898 : : unsigned int i;
899 : :
900 [ # # ]: 0 : if (current_group == previous_group)
901 : : return;
902 : :
903 : : /* Move keys back to previous group */
904 : 0 : for (i = current_group->num_rules - bin_size;
905 [ # # ]: 0 : i < current_group->num_rules; i++) {
906 : 0 : previous_group->key_idx[previous_group->num_rules] =
907 : 0 : current_group->key_idx[i];
908 : 0 : previous_group->value[previous_group->num_rules] =
909 : 0 : current_group->value[i];
910 : 0 : previous_group->bin_id[previous_group->num_rules] =
911 : 0 : current_group->bin_id[i];
912 : 0 : previous_group->num_rules++;
913 : : }
914 : :
915 : : /*
916 : : * Decrease number of rules after the move
917 : : * in the new group
918 : : */
919 : 0 : current_group->num_rules -= bin_size;
920 : : }
921 : :
922 : : /**
923 : : * Computes an updated table entry where the supplied key points to a new host.
924 : : * If no entry exists, one is inserted.
925 : : *
926 : : * This function does NOT modify the online table(s)
927 : : * This function DOES modify the offline table
928 : : *
929 : : * @param table
930 : : * EFD table to reference
931 : : * @param socket_id
932 : : * Socket ID to use to lookup existing values (ideally caller's socket id)
933 : : * @param key
934 : : * Key to insert
935 : : * @param value
936 : : * Value to associate with key
937 : : * @param chunk_id
938 : : * Chunk ID of the chunk that was modified
939 : : * @param group_id
940 : : * Group ID of the group that was modified
941 : : * @param bin_id
942 : : * Bin ID that was modified
943 : : * @param new_bin_choice
944 : : * Newly chosen permutation which this bin will use
945 : : * @param entry
946 : : * Newly computed online entry to apply later with efd_apply_update
947 : : *
948 : : * @return
949 : : * RTE_EFD_UPDATE_WARN_GROUP_FULL
950 : : * Operation is insert, and the last available space in the
951 : : * key's group was just used. Future inserts may fail as groups fill up.
952 : : * This operation was still successful, and entry contains a valid update
953 : : * RTE_EFD_UPDATE_FAILED
954 : : * Either the EFD failed to find a suitable perfect hash or the group was full
955 : : * This is a fatal error, and the table is now in an indeterminate state
956 : : * RTE_EFD_UPDATE_NO_CHANGE
957 : : * Operation resulted in no change to the table (same value already exists)
958 : : * 0
959 : : * Insert or update was successful, and the new efd_online_group_entry
960 : : * is stored in *entry
961 : : *
962 : : * @warning
963 : : * Note that entry will be UNCHANGED if the update has no effect, and thus any
964 : : * subsequent use of the entry content will likely be invalid
965 : : */
966 : : static inline int
967 : 0 : efd_compute_update(struct rte_efd_table * const table,
968 : : const unsigned int socket_id, const void *key,
969 : : const efd_value_t value, uint32_t * const chunk_id,
970 : : uint32_t * const group_id, uint32_t * const bin_id,
971 : : uint8_t * const new_bin_choice,
972 : : struct efd_online_group_entry * const entry)
973 : : {
974 : : unsigned int i;
975 : : int ret;
976 : : uint32_t new_idx;
977 : 0 : void *new_k, *slot_id = NULL;
978 : : int status = EXIT_SUCCESS;
979 : : unsigned int found = 0;
980 : :
981 : 0 : efd_compute_ids(table, key, chunk_id, bin_id);
982 : :
983 : 0 : struct efd_offline_chunk_rules * const chunk =
984 : 0 : &table->offline_chunks[*chunk_id];
985 : : struct efd_offline_group_rules *new_group;
986 : :
987 : 0 : uint8_t current_choice = efd_get_choice(table, socket_id,
988 : : *chunk_id, *bin_id);
989 : 0 : uint32_t current_group_id = efd_bin_to_group[current_choice][*bin_id];
990 : 0 : struct efd_offline_group_rules * const current_group =
991 : : &chunk->group_rules[current_group_id];
992 : : uint8_t bin_size = 0;
993 : : uint8_t key_changed_index = 0;
994 : : efd_value_t key_changed_previous_value = 0;
995 : : uint32_t key_idx_previous = 0;
996 : :
997 : : /* Scan the current group and see if the key is already present */
998 [ # # ]: 0 : for (i = 0; i < current_group->num_rules; i++) {
999 [ # # ]: 0 : if (current_group->bin_id[i] == *bin_id)
1000 : 0 : bin_size++;
1001 : : else
1002 : 0 : continue;
1003 : :
1004 : 0 : void *key_stored = EFD_KEY(current_group->key_idx[i], table);
1005 [ # # # # ]: 0 : if (found == 0 && unlikely(memcmp(key_stored, key,
1006 : : table->key_len) == 0)) {
1007 : : /* Key is already present */
1008 : :
1009 : : /*
1010 : : * If previous value is same as new value,
1011 : : * no additional work is required
1012 : : */
1013 [ # # ]: 0 : if (current_group->value[i] == value)
1014 : : return RTE_EFD_UPDATE_NO_CHANGE;
1015 : :
1016 : : key_idx_previous = current_group->key_idx[i];
1017 : : key_changed_previous_value = current_group->value[i];
1018 : 0 : key_changed_index = i;
1019 : 0 : current_group->value[i] = value;
1020 : : found = 1;
1021 : : }
1022 : : }
1023 : :
1024 [ # # ]: 0 : if (found == 0) {
1025 : : /* Key does not exist. Insert the rule into the bin/group */
1026 [ # # ]: 0 : if (unlikely(current_group->num_rules >= EFD_MAX_GROUP_NUM_RULES)) {
1027 : 0 : EFD_LOG(ERR,
1028 : : "Fatal: No room remaining for insert into "
1029 : : "chunk %u group %u bin %u",
1030 : : *chunk_id,
1031 : : current_group_id, *bin_id);
1032 : 0 : return RTE_EFD_UPDATE_FAILED;
1033 : : }
1034 : :
1035 [ # # ]: 0 : if (unlikely(current_group->num_rules ==
1036 : : (EFD_MAX_GROUP_NUM_RULES - 1))) {
1037 : 0 : EFD_LOG(INFO, "Warn: Insert into last "
1038 : : "available slot in chunk %u "
1039 : : "group %u bin %u", *chunk_id,
1040 : : current_group_id, *bin_id);
1041 : : status = RTE_EFD_UPDATE_WARN_GROUP_FULL;
1042 : : }
1043 : :
1044 : 0 : if (rte_ring_sc_dequeue(table->free_slots, &slot_id) != 0)
1045 : 0 : return RTE_EFD_UPDATE_FAILED;
1046 : :
1047 : 0 : new_k = RTE_PTR_ADD(table->keys, (uintptr_t) slot_id *
1048 : : table->key_len);
1049 : : rte_prefetch0(new_k);
1050 : 0 : new_idx = (uint32_t) ((uintptr_t) slot_id);
1051 : :
1052 [ # # ]: 0 : rte_memcpy(EFD_KEY(new_idx, table), key, table->key_len);
1053 : 0 : current_group->key_idx[current_group->num_rules] = new_idx;
1054 : 0 : current_group->value[current_group->num_rules] = value;
1055 : 0 : current_group->bin_id[current_group->num_rules] = *bin_id;
1056 : 0 : current_group->num_rules++;
1057 : 0 : table->num_rules++;
1058 : 0 : bin_size++;
1059 : : } else {
1060 : 0 : uint32_t last = current_group->num_rules - 1;
1061 : : /* Swap the key with the last key inserted*/
1062 : 0 : current_group->key_idx[key_changed_index] =
1063 : 0 : current_group->key_idx[last];
1064 : 0 : current_group->value[key_changed_index] =
1065 : 0 : current_group->value[last];
1066 : 0 : current_group->bin_id[key_changed_index] =
1067 : 0 : current_group->bin_id[last];
1068 : :
1069 : : /*
1070 : : * Key to be updated will always be available
1071 : : * at the end of the group
1072 : : */
1073 : 0 : current_group->key_idx[last] = key_idx_previous;
1074 : 0 : current_group->value[last] = value;
1075 : 0 : current_group->bin_id[last] = *bin_id;
1076 : : }
1077 : :
1078 : 0 : *new_bin_choice = current_choice;
1079 : 0 : *group_id = current_group_id;
1080 : : new_group = current_group;
1081 : :
1082 : : /* Group need to be rebalanced when it starts to get loaded */
1083 [ # # ]: 0 : if (current_group->num_rules > EFD_MIN_BALANCED_NUM_RULES) {
1084 : :
1085 : : /*
1086 : : * Subtract the number of entries in the bin from
1087 : : * the original group
1088 : : */
1089 : 0 : current_group->num_rules -= bin_size;
1090 : :
1091 : : /*
1092 : : * Figure out which of the available groups that this bin
1093 : : * can map to is the smallest (using the current group
1094 : : * as baseline)
1095 : : */
1096 : : uint8_t smallest_choice = current_choice;
1097 : 0 : uint8_t smallest_size = current_group->num_rules;
1098 : : uint32_t smallest_group_id = current_group_id;
1099 : : unsigned char choice;
1100 : :
1101 [ # # ]: 0 : for (choice = 0; choice < EFD_CHUNK_NUM_BIN_TO_GROUP_SETS;
1102 : 0 : choice++) {
1103 : 0 : uint32_t test_group_id =
1104 : 0 : efd_bin_to_group[choice][*bin_id];
1105 : 0 : uint32_t num_rules =
1106 : : chunk->group_rules[test_group_id].num_rules;
1107 [ # # ]: 0 : if (num_rules < smallest_size) {
1108 : : smallest_choice = choice;
1109 : 0 : smallest_size = num_rules;
1110 : : smallest_group_id = test_group_id;
1111 : : }
1112 : : }
1113 : :
1114 : 0 : *new_bin_choice = smallest_choice;
1115 : 0 : *group_id = smallest_group_id;
1116 : 0 : new_group = &chunk->group_rules[smallest_group_id];
1117 : 0 : current_group->num_rules += bin_size;
1118 : :
1119 : : }
1120 : :
1121 : : uint8_t choice = 0;
1122 : : for (;;) {
1123 [ # # ]: 0 : if (current_group != new_group &&
1124 [ # # ]: 0 : new_group->num_rules + bin_size >
1125 : : EFD_MAX_GROUP_NUM_RULES) {
1126 : 0 : EFD_LOG(DEBUG,
1127 : : "Unable to move_groups to dest group "
1128 : : "containing %u entries."
1129 : : "bin_size:%u choice:%02x",
1130 : : new_group->num_rules, bin_size,
1131 : : choice - 1);
1132 : 0 : goto next_choice;
1133 : : }
1134 : 0 : move_groups(*bin_id, bin_size, new_group, current_group);
1135 : : /*
1136 : : * Recompute the hash function for the modified group,
1137 : : * and return it to the caller
1138 : : */
1139 : 0 : ret = efd_search_hash(table, new_group, entry);
1140 : :
1141 [ # # ]: 0 : if (!ret)
1142 : : return status;
1143 : :
1144 : 0 : EFD_LOG(DEBUG,
1145 : : "Failed to find perfect hash for group "
1146 : : "containing %u entries. bin_size:%u choice:%02x",
1147 : : new_group->num_rules, bin_size, choice - 1);
1148 : : /* Restore groups modified to their previous state */
1149 : 0 : revert_groups(current_group, new_group, bin_size);
1150 : :
1151 : 0 : next_choice:
1152 [ # # ]: 0 : if (choice == EFD_CHUNK_NUM_BIN_TO_GROUP_SETS)
1153 : : break;
1154 : 0 : *new_bin_choice = choice;
1155 : 0 : *group_id = efd_bin_to_group[choice][*bin_id];
1156 : 0 : new_group = &chunk->group_rules[*group_id];
1157 : 0 : choice++;
1158 : : }
1159 : :
1160 [ # # ]: 0 : if (!found) {
1161 : 0 : current_group->num_rules--;
1162 : 0 : table->num_rules--;
1163 : : } else
1164 : 0 : current_group->value[current_group->num_rules - 1] =
1165 : : key_changed_previous_value;
1166 : : return RTE_EFD_UPDATE_FAILED;
1167 : : }
1168 : :
1169 : : RTE_EXPORT_SYMBOL(rte_efd_update)
1170 : : int
1171 : 0 : rte_efd_update(struct rte_efd_table * const table, const unsigned int socket_id,
1172 : : const void *key, const efd_value_t value)
1173 : : {
1174 : 0 : uint32_t chunk_id = 0, group_id = 0, bin_id = 0;
1175 : 0 : uint8_t new_bin_choice = 0;
1176 : 0 : struct efd_online_group_entry entry = {{0}};
1177 : :
1178 : 0 : int status = efd_compute_update(table, socket_id, key, value,
1179 : : &chunk_id, &group_id, &bin_id,
1180 : : &new_bin_choice, &entry);
1181 : :
1182 [ # # ]: 0 : if (status == RTE_EFD_UPDATE_NO_CHANGE)
1183 : : return EXIT_SUCCESS;
1184 : :
1185 [ # # ]: 0 : if (status == RTE_EFD_UPDATE_FAILED)
1186 : : return status;
1187 : :
1188 : 0 : efd_apply_update(table, socket_id, chunk_id, group_id, bin_id,
1189 : : new_bin_choice, &entry);
1190 : 0 : return status;
1191 : : }
1192 : :
1193 : : RTE_EXPORT_SYMBOL(rte_efd_delete)
1194 : : int
1195 : 0 : rte_efd_delete(struct rte_efd_table * const table, const unsigned int socket_id,
1196 : : const void *key, efd_value_t * const prev_value)
1197 : : {
1198 : : unsigned int i;
1199 : : uint32_t chunk_id, bin_id;
1200 : : uint8_t not_found = 1;
1201 : :
1202 : 0 : efd_compute_ids(table, key, &chunk_id, &bin_id);
1203 : :
1204 : 0 : struct efd_offline_chunk_rules * const chunk =
1205 : 0 : &table->offline_chunks[chunk_id];
1206 : :
1207 : 0 : uint8_t current_choice = efd_get_choice(table, socket_id,
1208 : : chunk_id, bin_id);
1209 : 0 : uint32_t current_group_id = efd_bin_to_group[current_choice][bin_id];
1210 : : struct efd_offline_group_rules * const current_group =
1211 : : &chunk->group_rules[current_group_id];
1212 : :
1213 : : /*
1214 : : * Search the current group for the specified key.
1215 : : * If it exists, remove it and re-pack the other values
1216 : : */
1217 [ # # ]: 0 : for (i = 0; i < current_group->num_rules; i++) {
1218 [ # # ]: 0 : if (not_found) {
1219 : : /* Found key that needs to be removed */
1220 : 0 : if (memcmp(EFD_KEY(current_group->key_idx[i], table),
1221 [ # # ]: 0 : key, table->key_len) == 0) {
1222 : : /* Store previous value if requested by caller */
1223 [ # # ]: 0 : if (prev_value != NULL)
1224 : 0 : *prev_value = current_group->value[i];
1225 : :
1226 : : not_found = 0;
1227 : 0 : rte_ring_sp_enqueue(table->free_slots,
1228 : 0 : (void *)((uintptr_t)current_group->key_idx[i]));
1229 : : }
1230 : : } else {
1231 : : /*
1232 : : * If the desired key has been found,
1233 : : * need to shift other values up one
1234 : : */
1235 : :
1236 : : /* Need to shift this entry back up one index */
1237 : 0 : current_group->key_idx[i - 1] = current_group->key_idx[i];
1238 : 0 : current_group->value[i - 1] = current_group->value[i];
1239 : 0 : current_group->bin_id[i - 1] = current_group->bin_id[i];
1240 : : }
1241 : : }
1242 : :
1243 [ # # ]: 0 : if (not_found == 0) {
1244 : 0 : table->num_rules--;
1245 : 0 : current_group->num_rules--;
1246 : : }
1247 : :
1248 : 0 : return not_found;
1249 : : }
1250 : :
1251 : : static inline efd_value_t
1252 : : efd_lookup_internal_scalar(const efd_hashfunc_t *group_hash_idx,
1253 : : const efd_lookuptbl_t *group_lookup_table,
1254 : : const uint32_t hash_val_a, const uint32_t hash_val_b)
1255 : : {
1256 : : efd_value_t value = 0;
1257 : : uint32_t i;
1258 : :
1259 [ # # # # ]: 0 : for (i = 0; i < RTE_EFD_VALUE_NUM_BITS; i++) {
1260 : 0 : value <<= 1;
1261 : 0 : uint32_t h = hash_val_a + (hash_val_b *
1262 : 0 : group_hash_idx[RTE_EFD_VALUE_NUM_BITS - i - 1]);
1263 : 0 : uint16_t bucket_idx = h >> EFD_LOOKUPTBL_SHIFT;
1264 : 0 : value |= (group_lookup_table[
1265 : 0 : RTE_EFD_VALUE_NUM_BITS - i - 1] >>
1266 : 0 : bucket_idx) & 0x1;
1267 : : }
1268 : :
1269 : : return value;
1270 : : }
1271 : :
1272 : :
1273 : : static inline efd_value_t
1274 : : efd_lookup_internal(const struct efd_online_group_entry * const group,
1275 : : const uint32_t hash_val_a, const uint32_t hash_val_b,
1276 : : enum efd_lookup_internal_function lookup_fn)
1277 : : {
1278 : : efd_value_t value = 0;
1279 : :
1280 : : switch (lookup_fn) {
1281 : :
1282 : : #if defined(RTE_ARCH_X86) && defined(CC_SUPPORT_AVX2)
1283 : : case EFD_LOOKUP_AVX2:
1284 : : return efd_lookup_internal_avx2(group->hash_idx,
1285 : : group->lookup_table,
1286 : : hash_val_a,
1287 : : hash_val_b);
1288 : : break;
1289 : : #endif
1290 : : #if defined(RTE_ARCH_ARM64)
1291 : : case EFD_LOOKUP_NEON:
1292 : : return efd_lookup_internal_neon(group->hash_idx,
1293 : : group->lookup_table,
1294 : : hash_val_a,
1295 : : hash_val_b);
1296 : : break;
1297 : : #endif
1298 : : case EFD_LOOKUP_SCALAR:
1299 : : /* Fall-through */
1300 : : default:
1301 : 0 : return efd_lookup_internal_scalar(group->hash_idx,
1302 : 0 : group->lookup_table,
1303 : : hash_val_a,
1304 : : hash_val_b);
1305 : : }
1306 : :
1307 : : return value;
1308 : : }
1309 : :
1310 : : RTE_EXPORT_SYMBOL(rte_efd_lookup)
1311 : : efd_value_t
1312 : 0 : rte_efd_lookup(const struct rte_efd_table * const table,
1313 : : const unsigned int socket_id, const void *key)
1314 : : {
1315 : : uint32_t chunk_id, group_id, bin_id;
1316 : : uint8_t bin_choice;
1317 : : const struct efd_online_group_entry *group;
1318 : 0 : const struct efd_online_chunk * const chunks = table->chunks[socket_id];
1319 : :
1320 : : /* Determine the chunk and group location for the given key */
1321 : 0 : efd_compute_ids(table, key, &chunk_id, &bin_id);
1322 : 0 : bin_choice = efd_get_choice(table, socket_id, chunk_id, bin_id);
1323 : 0 : group_id = efd_bin_to_group[bin_choice][bin_id];
1324 : : group = &chunks[chunk_id].groups[group_id];
1325 : :
1326 : 0 : return efd_lookup_internal(group,
1327 : 0 : EFD_HASHFUNCA(key, table),
1328 : 0 : EFD_HASHFUNCB(key, table),
1329 : : table->lookup_fn);
1330 : : }
1331 : :
1332 : : RTE_EXPORT_SYMBOL(rte_efd_lookup_bulk)
1333 : 0 : void rte_efd_lookup_bulk(const struct rte_efd_table * const table,
1334 : : const unsigned int socket_id, const int num_keys,
1335 : : const void **key_list, efd_value_t * const value_list)
1336 : : {
1337 : : int i;
1338 : : uint32_t chunk_id_list[RTE_EFD_BURST_MAX];
1339 : : uint32_t bin_id_list[RTE_EFD_BURST_MAX];
1340 : : uint8_t bin_choice_list[RTE_EFD_BURST_MAX];
1341 : : uint32_t group_id_list[RTE_EFD_BURST_MAX];
1342 : : struct efd_online_group_entry *group;
1343 : :
1344 : 0 : struct efd_online_chunk *chunks = table->chunks[socket_id];
1345 : :
1346 [ # # ]: 0 : for (i = 0; i < num_keys; i++) {
1347 : 0 : efd_compute_ids(table, key_list[i], &chunk_id_list[i],
1348 : : &bin_id_list[i]);
1349 : 0 : rte_prefetch0(&chunks[chunk_id_list[i]].bin_choice_list);
1350 : : }
1351 : :
1352 [ # # ]: 0 : for (i = 0; i < num_keys; i++) {
1353 : 0 : bin_choice_list[i] = efd_get_choice(table, socket_id,
1354 : : chunk_id_list[i], bin_id_list[i]);
1355 : 0 : group_id_list[i] =
1356 : 0 : efd_bin_to_group[bin_choice_list[i]][bin_id_list[i]];
1357 : 0 : group = &chunks[chunk_id_list[i]].groups[group_id_list[i]];
1358 : : rte_prefetch0(group);
1359 : : }
1360 : :
1361 [ # # ]: 0 : for (i = 0; i < num_keys; i++) {
1362 : 0 : group = &chunks[chunk_id_list[i]].groups[group_id_list[i]];
1363 : 0 : value_list[i] = efd_lookup_internal(group,
1364 : 0 : EFD_HASHFUNCA(key_list[i], table),
1365 : 0 : EFD_HASHFUNCB(key_list[i], table),
1366 : : table->lookup_fn);
1367 : : }
1368 : 0 : }
|