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_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 Contains metadata about the rules table. */
126 struct rte_lpm_rule_info {
127 uint32_t used_rules; /**< Used rules so far. */
128 uint32_t first_rule; /**< Indexes the first rule of a given depth. */
131 /** @internal LPM structure. */
133 TAILQ_ENTRY(rte_lpm) next; /**< Next in list. */
136 char name[RTE_LPM_NAMESIZE]; /**< Name of the lpm. */
137 int mem_location; /**< @deprecated @see RTE_LPM_HEAP and RTE_LPM_MEMZONE. */
138 uint32_t max_rules; /**< Max. balanced rules per lpm. */
139 struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH]; /**< Rule info table. */
142 struct rte_lpm_tbl24_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES] \
143 __rte_cache_aligned; /**< LPM tbl24 table. */
144 struct rte_lpm_tbl8_entry tbl8[RTE_LPM_TBL8_NUM_ENTRIES] \
145 __rte_cache_aligned; /**< LPM tbl8 table. */
146 struct rte_lpm_rule rules_tbl[0] \
147 __rte_cache_aligned; /**< LPM rules. */
151 * Create an LPM object.
156 * NUMA socket ID for LPM table memory allocation
158 * Maximum number of LPM rules that can be added
160 * This parameter is currently unused
162 * Handle to LPM object on success, NULL otherwise with rte_errno set
163 * to an appropriate values. Possible rte_errno values include:
164 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
165 * - E_RTE_SECONDARY - function was called from a secondary process instance
166 * - E_RTE_NO_TAILQ - no tailq list could be got for the lpm object list
167 * - EINVAL - invalid parameter passed to function
168 * - ENOSPC - the maximum number of memzones has already been allocated
169 * - EEXIST - a memzone with the same name already exists
170 * - ENOMEM - no appropriate memory area found in which to create memzone
173 rte_lpm_create(const char *name, int socket_id, int max_rules, int flags);
176 * Find an existing LPM object and return a pointer to it.
179 * Name of the lpm object as passed to rte_lpm_create()
181 * Pointer to lpm object or NULL if object not found with rte_errno
182 * set appropriately. Possible rte_errno values include:
183 * - ENOENT - required entry not available to return.
186 rte_lpm_find_existing(const char *name);
189 * Free an LPM object.
197 rte_lpm_free(struct rte_lpm *lpm);
200 * Add a rule to the LPM table.
205 * IP of the rule to be added to the LPM table
207 * Depth of the rule to be added to the LPM table
209 * Next hop of the rule to be added to the LPM table
211 * 0 on success, negative value otherwise
214 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t next_hop);
217 * Delete a rule from the LPM table.
222 * IP of the rule to be deleted from the LPM table
224 * Depth of the rule to be deleted from the LPM table
226 * 0 on success, negative value otherwise
229 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
232 * Delete all rules from the LPM table.
238 rte_lpm_delete_all(struct rte_lpm *lpm);
241 * Lookup an IP into the LPM table.
246 * IP to be looked up in the LPM table
248 * Next hop of the most specific rule found for IP (valid on lookup hit only)
250 * -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
253 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
255 unsigned tbl24_index = (ip >> 8);
258 /* DEBUG: Check user input arguments. */
259 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
261 /* Copy tbl24 entry */
262 tbl_entry = *(const uint16_t *)&lpm->tbl24[tbl24_index];
264 /* Copy tbl8 entry (only if needed) */
265 if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
266 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
268 unsigned tbl8_index = (uint8_t)ip +
269 ((uint8_t)tbl_entry * RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
271 tbl_entry = *(const uint16_t *)&lpm->tbl8[tbl8_index];
274 *next_hop = (uint8_t)tbl_entry;
275 return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
279 * Lookup multiple IP addresses in an LPM table. This may be implemented as a
280 * macro, so the address of the function should not be used.
285 * Array of IPs to be looked up in the LPM table
287 * Next hop of the most specific rule found for IP (valid on lookup hit only).
288 * This is an array of two byte values. The most significant byte in each
289 * value says whether the lookup was successful (bitmask
290 * RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
293 * Number of elements in ips (and next_hops) array to lookup. This should be a
294 * compile time constant, and divisible by 8 for best performance.
296 * -EINVAL for incorrect arguments, otherwise 0
298 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
299 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
302 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t * ips,
303 uint16_t * next_hops, const unsigned n)
306 unsigned tbl24_indexes[n];
308 /* DEBUG: Check user input arguments. */
309 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
310 (next_hops == NULL)), -EINVAL);
312 for (i = 0; i < n; i++) {
313 tbl24_indexes[i] = ips[i] >> 8;
316 for (i = 0; i < n; i++) {
317 /* Simply copy tbl24 entry to output */
318 next_hops[i] = *(const uint16_t *)&lpm->tbl24[tbl24_indexes[i]];
320 /* Overwrite output with tbl8 entry if needed */
321 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
322 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
324 unsigned tbl8_index = (uint8_t)ips[i] +
325 ((uint8_t)next_hops[i] *
326 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
328 next_hops[i] = *(const uint16_t *)&lpm->tbl8[tbl8_index];
338 #endif /* _RTE_LPM_H_ */