4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 * RTE Longest Prefix Match (LPM)
43 #include <sys/queue.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_byteorder.h>
48 #include <rte_config.h>
49 #include <rte_memory.h>
50 #include <rte_common.h>
52 #include <rte_compat.h>
58 /** Max number of characters in LPM name. */
59 #define RTE_LPM_NAMESIZE 32
61 /** Maximum depth value possible for IPv4 LPM. */
62 #define RTE_LPM_MAX_DEPTH 32
64 /** @internal Total number of tbl24 entries. */
65 #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
67 /** @internal Number of entries in a tbl8 group. */
68 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
70 /** @internal Max number of tbl8 groups in the tbl8. */
71 #define RTE_LPM_MAX_TBL8_NUM_GROUPS (1 << 24)
73 /** @internal Total number of tbl8 groups in the tbl8. */
74 #define RTE_LPM_TBL8_NUM_GROUPS 256
76 /** @internal Total number of tbl8 entries. */
77 #define RTE_LPM_TBL8_NUM_ENTRIES (RTE_LPM_TBL8_NUM_GROUPS * \
78 RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
80 /** @internal Macro to enable/disable run-time checks. */
81 #if defined(RTE_LIBRTE_LPM_DEBUG)
82 #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
83 if (cond) return (retval); \
86 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
89 /** @internal bitmask with valid and valid_group fields set */
90 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x03000000
92 /** Bitmask used to indicate successful lookup */
93 #define RTE_LPM_LOOKUP_SUCCESS 0x01000000
95 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
96 /** @internal Tbl24 entry structure. */
98 struct rte_lpm_tbl_entry_v20 {
100 * Stores Next hop (tbl8 or tbl24 when valid_group is not set) or
101 * a group index pointing to a tbl8 structure (tbl24 only, when
102 * valid_group is set)
109 /* Using single uint8_t to store 3 values. */
110 uint8_t valid :1; /**< Validation flag. */
113 * - valid_group == 0: entry stores a next hop
114 * - valid_group == 1: entry stores a group_index pointing to a tbl8
116 * - valid_group indicates whether the current tbl8 is in use or not
118 uint8_t valid_group :1;
119 uint8_t depth :6; /**< Rule depth. */
123 struct rte_lpm_tbl_entry {
125 * Stores Next hop (tbl8 or tbl24 when valid_group is not set) or
126 * a group index pointing to a tbl8 structure (tbl24 only, when
127 * valid_group is set)
129 uint32_t next_hop :24;
130 /* Using single uint8_t to store 3 values. */
131 uint32_t valid :1; /**< Validation flag. */
134 * - valid_group == 0: entry stores a next hop
135 * - valid_group == 1: entry stores a group_index pointing to a tbl8
137 * - valid_group indicates whether the current tbl8 is in use or not
139 uint32_t valid_group :1;
140 uint32_t depth :6; /**< Rule depth. */
145 struct rte_lpm_tbl_entry_v20 {
147 uint8_t valid_group :1;
156 struct rte_lpm_tbl_entry {
158 uint32_t valid_group :1;
160 uint32_t next_hop :24;
166 /** LPM configuration structure. */
167 struct rte_lpm_config {
168 uint32_t max_rules; /**< Max number of rules. */
169 uint32_t number_tbl8s; /**< Number of tbl8s to allocate. */
170 int flags; /**< This field is currently unused. */
173 /** @internal Rule structure. */
174 struct rte_lpm_rule_v20 {
175 uint32_t ip; /**< Rule IP address. */
176 uint8_t next_hop; /**< Rule next hop. */
179 struct rte_lpm_rule {
180 uint32_t ip; /**< Rule IP address. */
181 uint32_t next_hop; /**< Rule next hop. */
184 /** @internal Contains metadata about the rules table. */
185 struct rte_lpm_rule_info {
186 uint32_t used_rules; /**< Used rules so far. */
187 uint32_t first_rule; /**< Indexes the first rule of a given depth. */
190 /** @internal LPM structure. */
193 char name[RTE_LPM_NAMESIZE]; /**< Name of the lpm. */
194 uint32_t max_rules; /**< Max. balanced rules per lpm. */
195 struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH]; /**< Rule info table. */
198 struct rte_lpm_tbl_entry_v20 tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
199 __rte_cache_aligned; /**< LPM tbl24 table. */
200 struct rte_lpm_tbl_entry_v20 tbl8[RTE_LPM_TBL8_NUM_ENTRIES]
201 __rte_cache_aligned; /**< LPM tbl8 table. */
202 struct rte_lpm_rule_v20 rules_tbl[]
203 __rte_cache_aligned; /**< LPM rules. */
208 char name[RTE_LPM_NAMESIZE]; /**< Name of the lpm. */
209 uint32_t max_rules; /**< Max. balanced rules per lpm. */
210 uint32_t number_tbl8s; /**< Number of tbl8s. */
211 struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH]; /**< Rule info table. */
214 struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
215 __rte_cache_aligned; /**< LPM tbl24 table. */
216 struct rte_lpm_tbl_entry *tbl8; /**< LPM tbl8 table. */
217 struct rte_lpm_rule *rules_tbl; /**< LPM rules. */
221 * Create an LPM object.
226 * NUMA socket ID for LPM table memory allocation
228 * Structure containing the configuration
230 * Handle to LPM object on success, NULL otherwise with rte_errno set
231 * to an appropriate values. Possible rte_errno values include:
232 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
233 * - E_RTE_SECONDARY - function was called from a secondary process instance
234 * - EINVAL - invalid parameter passed to function
235 * - ENOSPC - the maximum number of memzones has already been allocated
236 * - EEXIST - a memzone with the same name already exists
237 * - ENOMEM - no appropriate memory area found in which to create memzone
240 rte_lpm_create(const char *name, int socket_id,
241 const struct rte_lpm_config *config);
243 rte_lpm_create_v20(const char *name, int socket_id, int max_rules, int flags);
245 rte_lpm_create_v1604(const char *name, int socket_id,
246 const struct rte_lpm_config *config);
249 * Find an existing LPM object and return a pointer to it.
252 * Name of the lpm object as passed to rte_lpm_create()
254 * Pointer to lpm object or NULL if object not found with rte_errno
255 * set appropriately. Possible rte_errno values include:
256 * - ENOENT - required entry not available to return.
259 rte_lpm_find_existing(const char *name);
261 rte_lpm_find_existing_v20(const char *name);
263 rte_lpm_find_existing_v1604(const char *name);
266 * Free an LPM object.
274 rte_lpm_free(struct rte_lpm *lpm);
276 rte_lpm_free_v20(struct rte_lpm_v20 *lpm);
278 rte_lpm_free_v1604(struct rte_lpm *lpm);
281 * Add a rule to the LPM table.
286 * IP of the rule to be added to the LPM table
288 * Depth of the rule to be added to the LPM table
290 * Next hop of the rule to be added to the LPM table
292 * 0 on success, negative value otherwise
295 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
297 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
300 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
304 * Check if a rule is present in the LPM table,
305 * and provide its next hop if it is.
310 * IP of the rule to be searched
312 * Depth of the rule to searched
314 * Next hop of the rule (valid only if it is found)
316 * 1 if the rule exists, 0 if it does not, a negative value on failure
319 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
322 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
325 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
329 * Delete a rule from the LPM table.
334 * IP of the rule to be deleted from the LPM table
336 * Depth of the rule to be deleted from the LPM table
338 * 0 on success, negative value otherwise
341 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
343 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth);
345 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
348 * Delete all rules from the LPM table.
354 rte_lpm_delete_all(struct rte_lpm *lpm);
356 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm);
358 rte_lpm_delete_all_v1604(struct rte_lpm *lpm);
361 * Lookup an IP into the LPM table.
366 * IP to be looked up in the LPM table
368 * Next hop of the most specific rule found for IP (valid on lookup hit only)
370 * -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
373 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
375 unsigned tbl24_index = (ip >> 8);
377 const uint32_t *ptbl;
379 /* DEBUG: Check user input arguments. */
380 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
382 /* Copy tbl24 entry */
383 ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
386 /* Copy tbl8 entry (only if needed) */
387 if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
388 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
390 unsigned tbl8_index = (uint8_t)ip +
391 (((uint32_t)tbl_entry & 0x00FFFFFF) *
392 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
394 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
398 *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
399 return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
403 * Lookup multiple IP addresses in an LPM table. This may be implemented as a
404 * macro, so the address of the function should not be used.
409 * Array of IPs to be looked up in the LPM table
411 * Next hop of the most specific rule found for IP (valid on lookup hit only).
412 * This is an array of two byte values. The most significant byte in each
413 * value says whether the lookup was successful (bitmask
414 * RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
417 * Number of elements in ips (and next_hops) array to lookup. This should be a
418 * compile time constant, and divisible by 8 for best performance.
420 * -EINVAL for incorrect arguments, otherwise 0
422 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
423 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
426 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
427 uint32_t *next_hops, const unsigned n)
430 unsigned tbl24_indexes[n];
431 const uint32_t *ptbl;
433 /* DEBUG: Check user input arguments. */
434 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
435 (next_hops == NULL)), -EINVAL);
437 for (i = 0; i < n; i++) {
438 tbl24_indexes[i] = ips[i] >> 8;
441 for (i = 0; i < n; i++) {
442 /* Simply copy tbl24 entry to output */
443 ptbl = (const uint32_t *)&lpm->tbl24[tbl24_indexes[i]];
444 next_hops[i] = *ptbl;
446 /* Overwrite output with tbl8 entry if needed */
447 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
448 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
450 unsigned tbl8_index = (uint8_t)ips[i] +
451 (((uint32_t)next_hops[i] & 0x00FFFFFF) *
452 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
454 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
455 next_hops[i] = *ptbl;
461 /* Mask four results. */
462 #define RTE_LPM_MASKX4_RES UINT64_C(0x00ffffff00ffffff)
465 * Lookup four IP addresses in an LPM table.
470 * Four IPs to be looked up in the LPM table
472 * Next hop of the most specific rule found for IP (valid on lookup hit only).
473 * This is an 4 elements array of two byte values.
474 * If the lookup was succesfull for the given IP, then least significant byte
475 * of the corresponding element is the actual next hop and the most
476 * significant byte is zero.
477 * If the lookup for the given IP failed, then corresponding element would
478 * contain default value, see description of then next parameter.
480 * Default value to populate into corresponding element of hop[] array,
481 * if lookup would fail.
484 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
487 #if defined(RTE_ARCH_ARM) || defined(RTE_ARCH_ARM64)
488 #include "rte_lpm_neon.h"
489 #elif defined(RTE_ARCH_PPC_64)
490 #include "rte_lpm_altivec.h"
492 #include "rte_lpm_sse.h"
499 #endif /* _RTE_LPM_H_ */