1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 * RTE Longest Prefix Match (LPM)
15 #include <sys/queue.h>
18 #include <rte_branch_prediction.h>
19 #include <rte_byteorder.h>
20 #include <rte_config.h>
21 #include <rte_memory.h>
22 #include <rte_common.h>
25 #include "rte_branch_prediction.h"
31 /** Max number of characters in LPM name. */
32 #define RTE_LPM_NAMESIZE 16
34 /** Maximum depth value possible for IPv4 LPM. */
35 #define RTE_LPM_MAX_DEPTH 32
37 /** @internal Total number of tbl24 entries. */
38 #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
40 /** @internal Number of entries in a tbl8 group. */
41 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
43 /** @internal Max number of tbl8 groups in the tbl8. */
44 #define RTE_LPM_MAX_TBL8_NUM_GROUPS (1 << 24)
46 /** @internal Total number of tbl8 groups in the tbl8. */
47 #define RTE_LPM_TBL8_NUM_GROUPS 256
49 /** @internal Total number of tbl8 entries. */
50 #define RTE_LPM_TBL8_NUM_ENTRIES (RTE_LPM_TBL8_NUM_GROUPS * \
51 RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
53 /** @internal Macro to enable/disable run-time checks. */
54 #if defined(RTE_LIBRTE_LPM_DEBUG)
55 #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
56 if (cond) return (retval); \
59 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
62 /** @internal bitmask with valid and valid_group fields set */
63 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x03000000
65 /** Bitmask used to indicate successful lookup */
66 #define RTE_LPM_LOOKUP_SUCCESS 0x01000000
68 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
69 /** @internal Tbl24 entry structure. */
71 struct rte_lpm_tbl_entry {
73 * Stores Next hop (tbl8 or tbl24 when valid_group is not set) or
74 * a group index pointing to a tbl8 structure (tbl24 only, when
77 uint32_t next_hop :24;
78 /* Using single uint8_t to store 3 values. */
79 uint32_t valid :1; /**< Validation flag. */
82 * - valid_group == 0: entry stores a next hop
83 * - valid_group == 1: entry stores a group_index pointing to a tbl8
85 * - valid_group indicates whether the current tbl8 is in use or not
87 uint32_t valid_group :1;
88 uint32_t depth :6; /**< Rule depth. */
94 struct rte_lpm_tbl_entry {
96 uint32_t valid_group :1;
98 uint32_t next_hop :24;
104 /** LPM configuration structure. */
105 struct rte_lpm_config {
106 uint32_t max_rules; /**< Max number of rules. */
107 uint32_t number_tbl8s; /**< Number of tbl8s to allocate. */
108 int flags; /**< This field is currently unused. */
111 /** @internal Rule structure. */
112 struct rte_lpm_rule {
113 uint32_t ip; /**< Rule IP address. */
114 uint32_t next_hop; /**< Rule next hop. */
117 /** @internal Contains metadata about the rules table. */
118 struct rte_lpm_rule_info {
119 uint32_t used_rules; /**< Used rules so far. */
120 uint32_t first_rule; /**< Indexes the first rule of a given depth. */
124 struct rte_lpm_external {
125 struct nhop_object **nh_idx; /**< # -> idx mappings */
126 uint32_t default_idx; /* nhop index of default route */
127 uint32_t fibnum; /* fib index */
130 /** @internal LPM structure. */
133 struct rte_lpm_external ext;
134 char name[RTE_LPM_NAMESIZE]; /**< Name of the lpm. */
135 uint32_t max_rules; /**< Max. balanced rules per lpm. */
136 uint32_t number_tbl8s; /**< Number of tbl8s. */
137 struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH]; /**< Rule info table. */
140 struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
141 __rte_cache_aligned; /**< LPM tbl24 table. */
142 struct rte_lpm_tbl_entry *tbl8; /**< LPM tbl8 table. */
143 struct rte_lpm_rule *rules_tbl; /**< LPM rules. */
147 * Create an LPM object.
152 * NUMA socket ID for LPM table memory allocation
154 * Structure containing the configuration
156 * Handle to LPM object on success, NULL otherwise with rte_errno set
157 * to an appropriate values. Possible rte_errno values include:
158 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
159 * - E_RTE_SECONDARY - function was called from a secondary process instance
160 * - EINVAL - invalid parameter passed to function
161 * - ENOSPC - the maximum number of memzones has already been allocated
162 * - EEXIST - a memzone with the same name already exists
163 * - ENOMEM - no appropriate memory area found in which to create memzone
166 rte_lpm_create(const char *name, int socket_id,
167 const struct rte_lpm_config *config);
170 * Find an existing LPM object and return a pointer to it.
173 * Name of the lpm object as passed to rte_lpm_create()
175 * Pointer to lpm object or NULL if object not found with rte_errno
176 * set appropriately. Possible rte_errno values include:
177 * - ENOENT - required entry not available to return.
180 rte_lpm_find_existing(const char *name);
183 * Free an LPM object.
191 rte_lpm_free(struct rte_lpm *lpm);
194 * Add a rule to the LPM table.
199 * IP of the rule to be added to the LPM table
201 * Depth of the rule to be added to the LPM table
203 * Next hop of the rule to be added to the LPM table
205 * 0 on success, negative value otherwise
208 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
211 * Check if a rule is present in the LPM table,
212 * and provide its next hop if it is.
217 * IP of the rule to be searched
219 * Depth of the rule to searched
221 * Next hop of the rule (valid only if it is found)
223 * 1 if the rule exists, 0 if it does not, a negative value on failure
226 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
230 * Delete a rule from the LPM table.
235 * IP of the rule to be deleted from the LPM table
237 * Depth of the rule to be deleted from the LPM table
238 * @param psub_rule_depth
239 * Pointer to depth of the parent rule
240 * @param sub_rule_nhop
241 * Pinter to the parent rule nexthop index
243 * 0 on success, negative value otherwise
246 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
247 uint8_t sub_rule_depth, uint32_t sub_rule_nhop);
250 * Delete all rules from the LPM table.
256 rte_lpm_delete_all(struct rte_lpm *lpm);
259 * Lookup an IP into the LPM table.
264 * IP to be looked up in the LPM table
266 * Next hop of the most specific rule found for IP (valid on lookup hit only)
268 * -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
271 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
273 unsigned tbl24_index = (ip >> 8);
275 const uint32_t *ptbl;
277 /* DEBUG: Check user input arguments. */
278 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
280 /* Copy tbl24 entry */
281 ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
284 /* Memory ordering is not required in lookup. Because dataflow
285 * dependency exists, compiler or HW won't be able to re-order
288 /* Copy tbl8 entry (only if needed) */
289 if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
290 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
292 unsigned tbl8_index = (uint8_t)ip +
293 (((uint32_t)tbl_entry & 0x00FFFFFF) *
294 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
296 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
300 *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
301 return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
305 * Lookup multiple IP addresses in an LPM table. This may be implemented as a
306 * macro, so the address of the function should not be used.
311 * Array of IPs to be looked up in the LPM table
313 * Next hop of the most specific rule found for IP (valid on lookup hit only).
314 * This is an array of two byte values. The most significant byte in each
315 * value says whether the lookup was successful (bitmask
316 * RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
319 * Number of elements in ips (and next_hops) array to lookup. This should be a
320 * compile time constant, and divisible by 8 for best performance.
322 * -EINVAL for incorrect arguments, otherwise 0
324 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
325 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
328 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
329 uint32_t *next_hops, const unsigned n)
332 unsigned tbl24_indexes[n];
333 const uint32_t *ptbl;
335 /* DEBUG: Check user input arguments. */
336 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
337 (next_hops == NULL)), -EINVAL);
339 for (i = 0; i < n; i++) {
340 tbl24_indexes[i] = ips[i] >> 8;
343 for (i = 0; i < n; i++) {
344 /* Simply copy tbl24 entry to output */
345 ptbl = (const uint32_t *)&lpm->tbl24[tbl24_indexes[i]];
346 next_hops[i] = *ptbl;
348 /* Overwrite output with tbl8 entry if needed */
349 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
350 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
352 unsigned tbl8_index = (uint8_t)ips[i] +
353 (((uint32_t)next_hops[i] & 0x00FFFFFF) *
354 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
356 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
357 next_hops[i] = *ptbl;
363 /* Mask four results. */
364 #define RTE_LPM_MASKX4_RES UINT64_C(0x00ffffff00ffffff)
367 * Lookup four IP addresses in an LPM table.
372 * Four IPs to be looked up in the LPM table
374 * Next hop of the most specific rule found for IP (valid on lookup hit only).
375 * This is an 4 elements array of two byte values.
376 * If the lookup was successful for the given IP, then least significant byte
377 * of the corresponding element is the actual next hop and the most
378 * significant byte is zero.
379 * If the lookup for the given IP failed, then corresponding element would
380 * contain default value, see description of then next parameter.
382 * Default value to populate into corresponding element of hop[] array,
383 * if lookup would fail.
387 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
390 #if defined(RTE_ARCH_ARM) || defined(RTE_ARCH_ARM64)
391 #include "rte_lpm_neon.h"
392 #elif defined(RTE_ARCH_PPC_64)
393 #include "rte_lpm_altivec.h"
395 #include "rte_lpm_sse.h"
403 #endif /* _RTE_LPM_H_ */