4 * Copyright(c) 2010-2013 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_memory.h>
53 /** Max number of characters in LPM name. */
54 #define RTE_LPM_NAMESIZE 32
56 /** @deprecated Possible location to allocate memory. This was for last
57 * parameter of rte_lpm_create(), but is now redundant. The LPM table is always
58 * allocated in memory using librte_malloc which uses a memzone. */
59 #define RTE_LPM_HEAP 0
61 /** @deprecated Possible location to allocate memory. This was for last
62 * parameter of rte_lpm_create(), but is now redundant. The LPM table is always
63 * allocated in memory using librte_malloc which uses a memzone. */
64 #define RTE_LPM_MEMZONE 1
66 /** Maximum depth value possible for IPv4 LPM. */
67 #define RTE_LPM_MAX_DEPTH 32
69 /** @internal Total number of tbl24 entries. */
70 #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
72 /** @internal Number of entries in a tbl8 group. */
73 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
75 /** @internal Total number of tbl8 groups in the tbl8. */
76 #define RTE_LPM_TBL8_NUM_GROUPS 256
78 /** @internal Total number of tbl8 entries. */
79 #define RTE_LPM_TBL8_NUM_ENTRIES (RTE_LPM_TBL8_NUM_GROUPS * \
80 RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
82 /** @internal Macro to enable/disable run-time checks. */
83 #if defined(RTE_LIBRTE_LPM_DEBUG)
84 #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
85 if (cond) return (retval); \
88 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
91 /** @internal bitmask with valid and ext_entry/valid_group fields set */
92 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x0300
94 /** Bitmask used to indicate successful lookup */
95 #define RTE_LPM_LOOKUP_SUCCESS 0x0100
97 /** @internal Tbl24 entry structure. */
98 struct rte_lpm_tbl24_entry {
99 /* Stores Next hop or group index (i.e. gindex)into tbl8. */
104 /* Using single uint8_t to store 3 values. */
105 uint8_t valid :1; /**< Validation flag. */
106 uint8_t ext_entry :1; /**< External entry. */
107 uint8_t depth :6; /**< Rule depth. */
110 /** @internal Tbl8 entry structure. */
111 struct rte_lpm_tbl8_entry {
112 uint8_t next_hop; /**< next hop. */
113 /* Using single uint8_t to store 3 values. */
114 uint8_t valid :1; /**< Validation flag. */
115 uint8_t valid_group :1; /**< Group validation flag. */
116 uint8_t depth :6; /**< Rule depth. */
119 /** @internal Rule structure. */
120 struct rte_lpm_rule {
121 uint32_t ip; /**< Rule IP address. */
122 uint8_t next_hop; /**< Rule next hop. */
125 /** @internal LPM structure. */
127 TAILQ_ENTRY(rte_lpm) next; /**< Next in list. */
130 char name[RTE_LPM_NAMESIZE]; /**< Name of the lpm. */
131 int mem_location; /**< @deprecated @see RTE_LPM_HEAP and RTE_LPM_MEMZONE. */
132 uint32_t max_rules_per_depth; /**< Max. balanced rules per lpm. */
133 uint32_t used_rules_at_depth[RTE_LPM_MAX_DEPTH]; /**< Rules / depth. */
136 struct rte_lpm_tbl24_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES] \
137 __rte_cache_aligned; /**< LPM tbl24 table. */
138 struct rte_lpm_tbl8_entry tbl8[RTE_LPM_TBL8_NUM_ENTRIES] \
139 __rte_cache_aligned; /**< LPM tbl8 table. */
140 struct rte_lpm_rule rules_tbl[0] \
141 __rte_cache_aligned; /**< LPM rules. */
145 * Create an LPM object.
150 * NUMA socket ID for LPM table memory allocation
152 * Maximum number of LPM rules that can be added
154 * This parameter is currently unused
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 * - E_RTE_NO_TAILQ - no tailq list could be got for the lpm object list
161 * - EINVAL - invalid parameter passed to function
162 * - ENOSPC - the maximum number of memzones has already been allocated
163 * - EEXIST - a memzone with the same name already exists
164 * - ENOMEM - no appropriate memory area found in which to create memzone
167 rte_lpm_create(const char *name, int socket_id, int max_rules, int flags);
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, uint8_t next_hop);
211 * Delete a rule from the LPM table.
216 * IP of the rule to be deleted from the LPM table
218 * Depth of the rule to be deleted from the LPM table
220 * 0 on success, negative value otherwise
223 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
226 * Delete all rules from the LPM table.
232 rte_lpm_delete_all(struct rte_lpm *lpm);
235 * Lookup an IP into the LPM table.
240 * IP to be looked up in the LPM table
242 * Next hop of the most specific rule found for IP (valid on lookup hit only)
244 * -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
247 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
249 unsigned tbl24_index = (ip >> 8);
252 /* DEBUG: Check user input arguments. */
253 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
255 /* Copy tbl24 entry */
256 tbl_entry = *(const uint16_t *)&lpm->tbl24[tbl24_index];
258 /* Copy tbl8 entry (only if needed) */
259 if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
260 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
262 unsigned tbl8_index = (uint8_t)ip +
263 ((uint8_t)tbl_entry * RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
265 tbl_entry = *(const uint16_t *)&lpm->tbl8[tbl8_index];
268 *next_hop = (uint8_t)tbl_entry;
269 return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
273 * Lookup multiple IP addresses in an LPM table. This may be implemented as a
274 * macro, so the address of the function should not be used.
279 * Array of IPs to be looked up in the LPM table
281 * Next hop of the most specific rule found for IP (valid on lookup hit only).
282 * This is an array of two byte values. The most significant byte in each
283 * value says whether the lookup was successful (bitmask
284 * RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
287 * Number of elements in ips (and next_hops) array to lookup. This should be a
288 * compile time constant, and divisible by 8 for best performance.
290 * -EINVAL for incorrect arguments, otherwise 0
292 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
293 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
296 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t * ips,
297 uint16_t * next_hops, const unsigned n)
300 unsigned tbl24_indexes[n];
302 /* DEBUG: Check user input arguments. */
303 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
304 (next_hops == NULL)), -EINVAL);
306 for (i = 0; i < n; i++) {
307 tbl24_indexes[i] = ips[i] >> 8;
310 for (i = 0; i < n; i++) {
311 /* Simply copy tbl24 entry to output */
312 next_hops[i] = *(const uint16_t *)&lpm->tbl24[tbl24_indexes[i]];
314 /* Overwrite output with tbl8 entry if needed */
315 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
316 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
318 unsigned tbl8_index = (uint8_t)ips[i] +
319 ((uint8_t)next_hops[i] *
320 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
322 next_hops[i] = *(const uint16_t *)&lpm->tbl8[tbl8_index];
332 #endif /* _RTE_LPM_H_ */