1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 * RTE Longest Prefix Match (LPM)
14 #include <sys/queue.h>
17 #include <rte_branch_prediction.h>
18 #include <rte_byteorder.h>
19 #include <rte_config.h>
20 #include <rte_memory.h>
21 #include <rte_common.h>
28 /** Max number of characters in LPM name. */
29 #define RTE_LPM_NAMESIZE 32
31 /** Maximum depth value possible for IPv4 LPM. */
32 #define RTE_LPM_MAX_DEPTH 32
34 /** @internal Total number of tbl24 entries. */
35 #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
37 /** @internal Number of entries in a tbl8 group. */
38 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
40 /** @internal Max number of tbl8 groups in the tbl8. */
41 #define RTE_LPM_MAX_TBL8_NUM_GROUPS (1 << 24)
43 /** @internal Total number of tbl8 groups in the tbl8. */
44 #define RTE_LPM_TBL8_NUM_GROUPS 256
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)
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); \
56 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
59 /** @internal bitmask with valid and valid_group fields set */
60 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x03000000
62 /** Bitmask used to indicate successful lookup */
63 #define RTE_LPM_LOOKUP_SUCCESS 0x01000000
65 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
66 /** @internal Tbl24 entry structure. */
68 struct rte_lpm_tbl_entry {
70 * Stores Next hop (tbl8 or tbl24 when valid_group is not set) or
71 * a group index pointing to a tbl8 structure (tbl24 only, when
74 uint32_t next_hop :24;
75 /* Using single uint8_t to store 3 values. */
76 uint32_t valid :1; /**< Validation flag. */
79 * - valid_group == 0: entry stores a next hop
80 * - valid_group == 1: entry stores a group_index pointing to a tbl8
82 * - valid_group indicates whether the current tbl8 is in use or not
84 uint32_t valid_group :1;
85 uint32_t depth :6; /**< Rule depth. */
91 struct rte_lpm_tbl_entry {
93 uint32_t valid_group :1;
95 uint32_t next_hop :24;
101 /** LPM configuration structure. */
102 struct rte_lpm_config {
103 uint32_t max_rules; /**< Max number of rules. */
104 uint32_t number_tbl8s; /**< Number of tbl8s to allocate. */
105 int flags; /**< This field is currently unused. */
108 /** @internal Rule structure. */
109 struct rte_lpm_rule {
110 uint32_t ip; /**< Rule IP address. */
111 uint32_t next_hop; /**< Rule next hop. */
114 /** @internal Contains metadata about the rules table. */
115 struct rte_lpm_rule_info {
116 uint32_t used_rules; /**< Used rules so far. */
117 uint32_t first_rule; /**< Indexes the first rule of a given depth. */
120 /** @internal LPM structure. */
123 char name[RTE_LPM_NAMESIZE]; /**< Name of the lpm. */
124 uint32_t max_rules; /**< Max. balanced rules per lpm. */
125 uint32_t number_tbl8s; /**< Number of tbl8s. */
126 struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH]; /**< Rule info table. */
129 struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
130 __rte_cache_aligned; /**< LPM tbl24 table. */
131 struct rte_lpm_tbl_entry *tbl8; /**< LPM tbl8 table. */
132 struct rte_lpm_rule *rules_tbl; /**< LPM rules. */
136 * Create an LPM object.
141 * NUMA socket ID for LPM table memory allocation
143 * Structure containing the configuration
145 * Handle to LPM object on success, NULL otherwise with rte_errno set
146 * to an appropriate values. Possible rte_errno values include:
147 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
148 * - E_RTE_SECONDARY - function was called from a secondary process instance
149 * - EINVAL - invalid parameter passed to function
150 * - ENOSPC - the maximum number of memzones has already been allocated
151 * - EEXIST - a memzone with the same name already exists
152 * - ENOMEM - no appropriate memory area found in which to create memzone
155 rte_lpm_create(const char *name, int socket_id,
156 const struct rte_lpm_config *config);
159 * Find an existing LPM object and return a pointer to it.
162 * Name of the lpm object as passed to rte_lpm_create()
164 * Pointer to lpm object or NULL if object not found with rte_errno
165 * set appropriately. Possible rte_errno values include:
166 * - ENOENT - required entry not available to return.
169 rte_lpm_find_existing(const char *name);
172 * Free an LPM object.
180 rte_lpm_free(struct rte_lpm *lpm);
183 * Add a rule to the LPM table.
188 * IP of the rule to be added to the LPM table
190 * Depth of the rule to be added to the LPM table
192 * Next hop of the rule to be added to the LPM table
194 * 0 on success, negative value otherwise
197 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
200 * Check if a rule is present in the LPM table,
201 * and provide its next hop if it is.
206 * IP of the rule to be searched
208 * Depth of the rule to searched
210 * Next hop of the rule (valid only if it is found)
212 * 1 if the rule exists, 0 if it does not, a negative value on failure
215 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
219 * Delete a rule from the LPM table.
224 * IP of the rule to be deleted from the LPM table
226 * Depth of the rule to be deleted from the LPM table
228 * 0 on success, negative value otherwise
231 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
234 * Delete all rules from the LPM table.
240 rte_lpm_delete_all(struct rte_lpm *lpm);
243 * Lookup an IP into the LPM table.
248 * IP to be looked up in the LPM table
250 * Next hop of the most specific rule found for IP (valid on lookup hit only)
252 * -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
255 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
257 unsigned tbl24_index = (ip >> 8);
259 const uint32_t *ptbl;
261 /* DEBUG: Check user input arguments. */
262 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
264 /* Copy tbl24 entry */
265 ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
268 /* Memory ordering is not required in lookup. Because dataflow
269 * dependency exists, compiler or HW won't be able to re-order
272 /* Copy tbl8 entry (only if needed) */
273 if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
274 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
276 unsigned tbl8_index = (uint8_t)ip +
277 (((uint32_t)tbl_entry & 0x00FFFFFF) *
278 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
280 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
284 *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
285 return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
289 * Lookup multiple IP addresses in an LPM table. This may be implemented as a
290 * macro, so the address of the function should not be used.
295 * Array of IPs to be looked up in the LPM table
297 * Next hop of the most specific rule found for IP (valid on lookup hit only).
298 * This is an array of two byte values. The most significant byte in each
299 * value says whether the lookup was successful (bitmask
300 * RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
303 * Number of elements in ips (and next_hops) array to lookup. This should be a
304 * compile time constant, and divisible by 8 for best performance.
306 * -EINVAL for incorrect arguments, otherwise 0
308 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
309 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
312 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
313 uint32_t *next_hops, const unsigned n)
316 unsigned tbl24_indexes[n];
317 const uint32_t *ptbl;
319 /* DEBUG: Check user input arguments. */
320 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
321 (next_hops == NULL)), -EINVAL);
323 for (i = 0; i < n; i++) {
324 tbl24_indexes[i] = ips[i] >> 8;
327 for (i = 0; i < n; i++) {
328 /* Simply copy tbl24 entry to output */
329 ptbl = (const uint32_t *)&lpm->tbl24[tbl24_indexes[i]];
330 next_hops[i] = *ptbl;
332 /* Overwrite output with tbl8 entry if needed */
333 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
334 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
336 unsigned tbl8_index = (uint8_t)ips[i] +
337 (((uint32_t)next_hops[i] & 0x00FFFFFF) *
338 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
340 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
341 next_hops[i] = *ptbl;
347 /* Mask four results. */
348 #define RTE_LPM_MASKX4_RES UINT64_C(0x00ffffff00ffffff)
351 * Lookup four IP addresses in an LPM table.
356 * Four IPs to be looked up in the LPM table
358 * Next hop of the most specific rule found for IP (valid on lookup hit only).
359 * This is an 4 elements array of two byte values.
360 * If the lookup was successful for the given IP, then least significant byte
361 * of the corresponding element is the actual next hop and the most
362 * significant byte is zero.
363 * If the lookup for the given IP failed, then corresponding element would
364 * contain default value, see description of then next parameter.
366 * Default value to populate into corresponding element of hop[] array,
367 * if lookup would fail.
370 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
373 #if defined(RTE_ARCH_ARM) || defined(RTE_ARCH_ARM64)
374 #include "rte_lpm_neon.h"
375 #elif defined(RTE_ARCH_PPC_64)
376 #include "rte_lpm_altivec.h"
378 #include "rte_lpm_sse.h"
385 #endif /* _RTE_LPM_H_ */