Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : * Copyright(c) 2020 Arm Limited
4 : : */
5 : :
6 : : #ifndef _RTE_LPM_H_
7 : : #define _RTE_LPM_H_
8 : :
9 : : /**
10 : : * @file
11 : : * RTE Longest Prefix Match (LPM)
12 : : */
13 : :
14 : : #include <errno.h>
15 : : #include <stdalign.h>
16 : : #include <stdint.h>
17 : :
18 : : #include <rte_branch_prediction.h>
19 : : #include <rte_byteorder.h>
20 : : #include <rte_common.h>
21 : : #include <rte_vect.h>
22 : : #include <rte_rcu_qsbr.h>
23 : :
24 : : #ifdef __cplusplus
25 : : extern "C" {
26 : : #endif
27 : :
28 : : /** Max number of characters in LPM name. */
29 : : #define RTE_LPM_NAMESIZE 32
30 : :
31 : : /** Maximum depth value possible for IPv4 LPM. */
32 : : #define RTE_LPM_MAX_DEPTH 32
33 : :
34 : : /** @internal Total number of tbl24 entries. */
35 : : #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
36 : :
37 : : /** @internal Number of entries in a tbl8 group. */
38 : : #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
39 : :
40 : : /** @internal Max number of tbl8 groups in the tbl8. */
41 : : #define RTE_LPM_MAX_TBL8_NUM_GROUPS (1 << 24)
42 : :
43 : : /** @internal Total number of tbl8 groups in the tbl8. */
44 : : #define RTE_LPM_TBL8_NUM_GROUPS 256
45 : :
46 : : /** @internal Total number of tbl8 entries. */
47 : : #define RTE_LPM_TBL8_NUM_ENTRIES (RTE_LPM_TBL8_NUM_GROUPS * \
48 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
49 : :
50 : : /** @internal Macro to enable/disable run-time checks. */
51 : : #if defined(RTE_LIBRTE_LPM_DEBUG)
52 : : #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
53 : : if (cond) return (retval); \
54 : : } while (0)
55 : : #else
56 : : #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
57 : : #endif
58 : :
59 : : /** @internal bitmask with valid and valid_group fields set */
60 : : #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x03000000
61 : :
62 : : /** Bitmask used to indicate successful lookup */
63 : : #define RTE_LPM_LOOKUP_SUCCESS 0x01000000
64 : :
65 : : /** @internal Default RCU defer queue entries to reclaim in one go. */
66 : : #define RTE_LPM_RCU_DQ_RECLAIM_MAX 16
67 : :
68 : : /** RCU reclamation modes */
69 : : enum rte_lpm_qsbr_mode {
70 : : /** Create defer queue for reclaim. */
71 : : RTE_LPM_QSBR_MODE_DQ = 0,
72 : : /** Use blocking mode reclaim. No defer queue created. */
73 : : RTE_LPM_QSBR_MODE_SYNC
74 : : };
75 : :
76 : : #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
77 : : /** @internal Tbl24 entry structure. */
78 : : __extension__
79 : : struct rte_lpm_tbl_entry {
80 : : union {
81 : : RTE_ATOMIC(uint32_t) val;
82 : : struct {
83 : : /**
84 : : * Stores Next hop (tbl8 or tbl24 when valid_group is not set) or
85 : : * a group index pointing to a tbl8 structure (tbl24 only, when
86 : : * valid_group is set)
87 : : */
88 : : uint32_t next_hop :24;
89 : : /* Using single uint8_t to store 3 values. */
90 : : uint32_t valid :1; /**< Validation flag. */
91 : : /**
92 : : * For tbl24:
93 : : * - valid_group == 0: entry stores a next hop
94 : : * - valid_group == 1: entry stores a group_index pointing to a tbl8
95 : : * For tbl8:
96 : : * - valid_group indicates whether the current tbl8 is in use or not
97 : : */
98 : : uint32_t valid_group :1;
99 : : uint32_t depth :6; /**< Rule depth. */
100 : : };
101 : : };
102 : : };
103 : :
104 : : #else
105 : :
106 : : __extension__
107 : : struct rte_lpm_tbl_entry {
108 : : union {
109 : : RTE_ATOMIC(uint32_t) val;
110 : : struct {
111 : : uint32_t depth :6;
112 : : uint32_t valid_group :1;
113 : : uint32_t valid :1;
114 : : uint32_t next_hop :24;
115 : : };
116 : : };
117 : : };
118 : :
119 : : #endif
120 : :
121 : : static_assert(sizeof(struct rte_lpm_tbl_entry) == sizeof(uint32_t),
122 : : "sizeof(struct rte_lpm_tbl_entry) == sizeof(uint32_t)");
123 : :
124 : : /** LPM configuration structure. */
125 : : struct rte_lpm_config {
126 : : uint32_t max_rules; /**< Max number of rules. */
127 : : uint32_t number_tbl8s; /**< Number of tbl8s to allocate. */
128 : : int flags; /**< This field is currently unused. */
129 : : };
130 : :
131 : : /** @internal LPM structure. */
132 : : struct rte_lpm {
133 : : /* LPM Tables. */
134 : : alignas(RTE_CACHE_LINE_SIZE) struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES];
135 : : /**< LPM tbl24 table. */
136 : : struct rte_lpm_tbl_entry *tbl8; /**< LPM tbl8 table. */
137 : : };
138 : :
139 : : /** LPM RCU QSBR configuration structure. */
140 : : struct rte_lpm_rcu_config {
141 : : struct rte_rcu_qsbr *v; /* RCU QSBR variable. */
142 : : /* Mode of RCU QSBR. RTE_LPM_QSBR_MODE_xxx
143 : : * '0' for default: create defer queue for reclaim.
144 : : */
145 : : enum rte_lpm_qsbr_mode mode;
146 : : uint32_t dq_size; /* RCU defer queue size.
147 : : * default: lpm->number_tbl8s.
148 : : */
149 : : uint32_t reclaim_thd; /* Threshold to trigger auto reclaim. */
150 : : uint32_t reclaim_max; /* Max entries to reclaim in one go.
151 : : * default: RTE_LPM_RCU_DQ_RECLAIM_MAX.
152 : : */
153 : : };
154 : :
155 : : /**
156 : : * Free an LPM object.
157 : : *
158 : : * @param lpm
159 : : * LPM object handle
160 : : * If lpm is NULL, no operation is performed.
161 : : */
162 : : void
163 : : rte_lpm_free(struct rte_lpm *lpm);
164 : :
165 : : /**
166 : : * Create an LPM object.
167 : : *
168 : : * @param name
169 : : * LPM object name
170 : : * @param socket_id
171 : : * NUMA socket ID for LPM table memory allocation
172 : : * @param config
173 : : * Structure containing the configuration
174 : : * @return
175 : : * Handle to LPM object on success, NULL otherwise with rte_errno set
176 : : * to an appropriate values. Possible rte_errno values include:
177 : : * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
178 : : * - E_RTE_SECONDARY - function was called from a secondary process instance
179 : : * - EINVAL - invalid parameter passed to function
180 : : * - ENOSPC - the maximum number of memzones has already been allocated
181 : : * - EEXIST - a memzone with the same name already exists
182 : : * - ENOMEM - no appropriate memory area found in which to create memzone
183 : : * - ENAMETOOLONG - LPM object name greater than RTE_LPM_NAMESIZE
184 : : */
185 : : struct rte_lpm *
186 : : rte_lpm_create(const char *name, int socket_id,
187 : : const struct rte_lpm_config *config)
188 : : __rte_malloc __rte_dealloc(rte_lpm_free, 1);
189 : :
190 : : /**
191 : : * Find an existing LPM object and return a pointer to it.
192 : : *
193 : : * @param name
194 : : * Name of the lpm object as passed to rte_lpm_create()
195 : : * @return
196 : : * Pointer to lpm object or NULL if object not found with rte_errno
197 : : * set appropriately. Possible rte_errno values include:
198 : : * - ENOENT - required entry not available to return.
199 : : */
200 : : struct rte_lpm *
201 : : rte_lpm_find_existing(const char *name);
202 : :
203 : : /**
204 : : * Associate RCU QSBR variable with an LPM object.
205 : : *
206 : : * @param lpm
207 : : * the lpm object to add RCU QSBR
208 : : * @param cfg
209 : : * RCU QSBR configuration
210 : : * @return
211 : : * On success - 0
212 : : * On error - 1 with error code set in rte_errno.
213 : : * Possible rte_errno codes are:
214 : : * - EINVAL - invalid pointer
215 : : * - EEXIST - already added QSBR
216 : : * - ENOMEM - memory allocation failure
217 : : */
218 : : int rte_lpm_rcu_qsbr_add(struct rte_lpm *lpm, struct rte_lpm_rcu_config *cfg);
219 : :
220 : : /**
221 : : * Add a rule to the LPM table.
222 : : *
223 : : * @param lpm
224 : : * LPM object handle
225 : : * @param ip
226 : : * IP of the rule to be added to the LPM table
227 : : * @param depth
228 : : * Depth of the rule to be added to the LPM table
229 : : * @param next_hop
230 : : * Next hop of the rule to be added to the LPM table
231 : : * @return
232 : : * 0 on success, negative value otherwise
233 : : */
234 : : int
235 : : rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
236 : :
237 : : /**
238 : : * Check if a rule is present in the LPM table,
239 : : * and provide its next hop if it is.
240 : : *
241 : : * @param lpm
242 : : * LPM object handle
243 : : * @param ip
244 : : * IP of the rule to be searched
245 : : * @param depth
246 : : * Depth of the rule to searched
247 : : * @param next_hop
248 : : * Next hop of the rule (valid only if it is found)
249 : : * @return
250 : : * 1 if the rule exists, 0 if it does not, a negative value on failure
251 : : */
252 : : int
253 : : rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
254 : : uint32_t *next_hop);
255 : :
256 : : /**
257 : : * Delete a rule from the LPM table.
258 : : *
259 : : * @param lpm
260 : : * LPM object handle
261 : : * @param ip
262 : : * IP of the rule to be deleted from the LPM table
263 : : * @param depth
264 : : * Depth of the rule to be deleted from the LPM table
265 : : * @return
266 : : * 0 on success, negative value otherwise
267 : : */
268 : : int
269 : : rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
270 : :
271 : : /**
272 : : * Delete all rules from the LPM table.
273 : : *
274 : : * @param lpm
275 : : * LPM object handle
276 : : */
277 : : void
278 : : rte_lpm_delete_all(struct rte_lpm *lpm);
279 : :
280 : : /**
281 : : * Lookup an IP into the LPM table.
282 : : *
283 : : * @param lpm
284 : : * LPM object handle
285 : : * @param ip
286 : : * IP to be looked up in the LPM table
287 : : * @param next_hop
288 : : * Next hop of the most specific rule found for IP (valid on lookup hit only)
289 : : * @return
290 : : * -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
291 : : */
292 : : static inline int
293 : : rte_lpm_lookup(const struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
294 : : {
295 : 778 : unsigned tbl24_index = (ip >> 8);
296 : : uint32_t tbl_entry;
297 : : const uint32_t *ptbl;
298 : :
299 : : /* DEBUG: Check user input arguments. */
300 : : RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
301 : :
302 : : /* Copy tbl24 entry */
303 : 778 : ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
304 : 4949 : tbl_entry = *ptbl;
305 : :
306 : : /* Memory ordering is not required in lookup. Because dataflow
307 : : * dependency exists, compiler or HW won't be able to re-order
308 : : * the operations.
309 : : */
310 : : /* Copy tbl8 entry (only if needed) */
311 [ + + - + : 4953 : if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
+ - + - +
- + - + -
+ - + - -
+ + - - +
- + + - -
+ + - + -
+ - - + -
+ - + + -
+ - + - +
- - + - +
- + - + +
- + - - +
- + - + -
+ - + - +
- + - + +
- + - + -
+ - - + +
- + - + -
- + - + +
+ + + - +
+ - - + -
+ - + ]
312 : : RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
313 : :
314 : 11 : unsigned tbl8_index = (uint8_t)ip +
315 : 2551 : (((uint32_t)tbl_entry & 0x00FFFFFF) *
316 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
317 : :
318 : 2551 : ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
319 : 2551 : tbl_entry = *ptbl;
320 : : }
321 : :
322 : 3871 : *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
323 [ + + + - : 4953 : return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
- + + - -
+ - + - +
- + + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - - + +
- + + + -
+ - + - +
- + - ]
324 : : }
325 : :
326 : : /**
327 : : * Lookup multiple IP addresses in an LPM table. This may be implemented as a
328 : : * macro, so the address of the function should not be used.
329 : : *
330 : : * @param lpm
331 : : * LPM object handle
332 : : * @param ips
333 : : * Array of IPs to be looked up in the LPM table
334 : : * @param next_hops
335 : : * Next hop of the most specific rule found for IP (valid on lookup hit only).
336 : : * This is an array of two byte values. The most significant byte in each
337 : : * value says whether the lookup was successful (bitmask
338 : : * RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
339 : : * actual next hop.
340 : : * @param n
341 : : * Number of elements in ips (and next_hops) array to lookup. This should be a
342 : : * compile time constant, and divisible by 8 for best performance.
343 : : * @return
344 : : * -EINVAL for incorrect arguments, otherwise 0
345 : : */
346 : : #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
347 : : rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
348 : :
349 : : static inline int
350 : 0 : rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
351 : : uint32_t *next_hops, const unsigned n)
352 : : {
353 : : unsigned i;
354 : : const uint32_t *ptbl;
355 : :
356 : : /* DEBUG: Check user input arguments. */
357 : : RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
358 : : (next_hops == NULL)), -EINVAL);
359 : :
360 [ # # ]: 0 : for (i = 0; i < n; i++) {
361 : 0 : unsigned int tbl24_index = ips[i] >> 8;
362 : :
363 : : /* Simply copy tbl24 entry to output */
364 : 0 : ptbl = (const uint32_t *)&lpm->tbl24[tbl24_index];
365 : 0 : next_hops[i] = *ptbl;
366 : :
367 : : /* Overwrite output with tbl8 entry if needed */
368 [ # # ]: 0 : if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
369 : : RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
370 : :
371 : 0 : unsigned tbl8_index = (uint8_t)ips[i] +
372 : 0 : (((uint32_t)next_hops[i] & 0x00FFFFFF) *
373 : : RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
374 : :
375 : 0 : ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
376 : 0 : next_hops[i] = *ptbl;
377 : : }
378 : : }
379 : 0 : return 0;
380 : : }
381 : :
382 : : /* Mask four results. */
383 : : #define RTE_LPM_MASKX4_RES UINT64_C(0x00ffffff00ffffff)
384 : :
385 : : /**
386 : : * Lookup four IP addresses in an LPM table.
387 : : *
388 : : * @param lpm
389 : : * LPM object handle
390 : : * @param ip
391 : : * Four IPs to be looked up in the LPM table
392 : : * @param hop
393 : : * Next hop of the most specific rule found for IP (valid on lookup hit only).
394 : : * This is an 4 elements array of two byte values.
395 : : * If the lookup was successful for the given IP, then least significant byte
396 : : * of the corresponding element is the actual next hop and the most
397 : : * significant byte is zero.
398 : : * If the lookup for the given IP failed, then corresponding element would
399 : : * contain default value, see description of then next parameter.
400 : : * @param defv
401 : : * Default value to populate into corresponding element of hop[] array,
402 : : * if lookup would fail.
403 : : */
404 : : static inline void
405 : : rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
406 : : uint32_t defv);
407 : :
408 : : #ifdef __cplusplus
409 : : }
410 : : #endif
411 : :
412 : : #if defined(RTE_ARCH_ARM)
413 : : #ifdef RTE_HAS_SVE_ACLE
414 : : #include "rte_lpm_sve.h"
415 : : #undef rte_lpm_lookup_bulk
416 : : #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
417 : : __rte_lpm_lookup_vec(lpm, ips, next_hops, n)
418 : : #endif
419 : : #include "rte_lpm_neon.h"
420 : : #elif defined(RTE_ARCH_PPC_64)
421 : : #include "rte_lpm_altivec.h"
422 : : #elif defined(RTE_ARCH_X86)
423 : : #include "rte_lpm_sse.h"
424 : : #elif defined(RTE_ARCH_RISCV) && defined(RTE_RISCV_FEATURE_V)
425 : : #include "rte_lpm_rvv.h"
426 : : #else
427 : : #include "rte_lpm_scalar.h"
428 : : #endif
429 : :
430 : : #endif /* _RTE_LPM_H_ */
|