4 * Copyright(c) 2010-2012 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.
40 * RTE Longest Prefix Match (LPM)
44 #include <sys/queue.h>
47 #include <rte_branch_prediction.h>
48 #include <rte_memory.h>
54 /** Max number of characters in LPM name. */
55 #define RTE_LPM_NAMESIZE 32
57 /** @deprecated Possible location to allocate memory. This was for last
58 * parameter of rte_lpm_create(), but is now redundant. The LPM table is always
59 * allocated in memory using librte_malloc which uses a memzone. */
60 #define RTE_LPM_HEAP 0
62 /** @deprecated Possible location to allocate memory. This was for last
63 * parameter of rte_lpm_create(), but is now redundant. The LPM table is always
64 * allocated in memory using librte_malloc which uses a memzone. */
65 #define RTE_LPM_MEMZONE 1
67 /** Maximum depth value possible for IPv4 LPM. */
68 #define RTE_LPM_MAX_DEPTH 32
70 /** @internal Total number of tbl24 entries. */
71 #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
73 /** @internal Number of entries in a tbl8 group. */
74 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
76 /** @internal Total number of tbl8 groups in the tbl8. */
77 #define RTE_LPM_TBL8_NUM_GROUPS 256
79 /** @internal Total number of tbl8 entries. */
80 #define RTE_LPM_TBL8_NUM_ENTRIES (RTE_LPM_TBL8_NUM_GROUPS * \
81 RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
83 /** @internal Macro to enable/disable run-time checks. */
84 #if defined(RTE_LIBRTE_LPM_DEBUG)
85 #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
86 if (cond) return (retval); \
89 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
92 /** @internal bitmask with valid and ext_entry/valid_group fields set */
93 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x0300
95 /** Bitmask used to indicate successful lookup */
96 #define RTE_LPM_LOOKUP_SUCCESS 0x0100
98 /** @internal Tbl24 entry structure. */
99 struct rte_lpm_tbl24_entry {
100 /* Using single uint8_t to store 3 values. */
101 uint8_t valid :1; /**< Validation flag. */
102 uint8_t ext_entry :1; /**< External entry. */
103 uint8_t depth :6; /**< Rule depth. */
104 /* Stores Next hop or group index (i.e. gindex)into tbl8. */
111 /** @internal Tbl8 entry structure. */
112 struct rte_lpm_tbl8_entry {
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. */
117 uint8_t next_hop; /**< next hop. */
120 /** @internal Rule structure. */
121 struct rte_lpm_rule {
122 uint32_t ip; /**< Rule IP address. */
123 uint8_t next_hop; /**< Rule next hop. */
126 /** @internal LPM structure. */
128 TAILQ_ENTRY(rte_lpm) next; /**< Next in list. */
131 char name[RTE_LPM_NAMESIZE]; /**< Name of the lpm. */
132 int mem_location; /**< @deprecated @see RTE_LPM_HEAP and RTE_LPM_MEMZONE. */
133 uint32_t max_rules_per_depth; /**< Max. balanced rules per lpm. */
134 uint32_t used_rules_at_depth[RTE_LPM_MAX_DEPTH]; /**< Rules / depth. */
137 struct rte_lpm_tbl24_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES] \
138 __rte_cache_aligned; /**< LPM tbl24 table. */
139 struct rte_lpm_tbl8_entry tbl8[RTE_LPM_TBL8_NUM_ENTRIES] \
140 __rte_cache_aligned; /**< LPM tbl8 table. */
141 struct rte_lpm_rule rules_tbl[0] \
142 __rte_cache_aligned; /**< LPM rules. */
146 * Create an LPM object.
151 * NUMA socket ID for LPM table memory allocation
153 * Maximum number of LPM rules that can be added
154 * @param mem_location
155 * Location of memory to be allocated. Can only be RTE_LPM_HEAP or
158 * Handle to LPM object on success, NULL otherwise with rte_errno set
159 * to an appropriate values. Possible rte_errno values include:
160 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
161 * - E_RTE_SECONDARY - function was called from a secondary process instance
162 * - E_RTE_NO_TAILQ - no tailq list could be got for the lpm object list
163 * - EINVAL - invalid parameter passed to function
164 * - ENOSPC - the maximum number of memzones has already been allocated
165 * - EEXIST - a memzone with the same name already exists
166 * - ENOMEM - no appropriate memory area found in which to create memzone
169 rte_lpm_create(const char *name, int socket_id, int max_rules,
173 * Find an existing LPM object and return a pointer to it.
176 * Name of the lpm object as passed to rte_lpm_create()
178 * Pointer to lpm object or NULL if object not found with rte_errno
179 * set appropriately. Possible rte_errno values include:
180 * - ENOENT - required entry not available to return.
183 rte_lpm_find_existing(const char *name);
186 * Free an LPM object.
194 rte_lpm_free(struct rte_lpm *lpm);
197 * Add a rule to the LPM table.
202 * IP of the rule to be added to the LPM table
204 * Depth of the rule to be added to the LPM table
206 * Next hop of the rule to be added to the LPM table
208 * 0 on success, negative value otherwise
211 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t next_hop);
214 * Delete a rule from the LPM table.
219 * IP of the rule to be deleted from the LPM table
221 * Depth of the rule to be deleted from the LPM table
223 * 0 on success, negative value otherwise
226 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
229 * Delete all rules from the LPM table.
235 rte_lpm_delete_all(struct rte_lpm *lpm);
238 * Lookup an IP into the LPM table.
243 * IP to be looked up in the LPM table
245 * Next hop of the most specific rule found for IP (valid on lookup hit only)
247 * -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
250 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
252 uint32_t tbl24_index, tbl8_group_index, tbl8_index;
254 /* DEBUG: Check user input arguments. */
255 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
257 /* Calculate index into tbl24. */
258 tbl24_index = (ip >> 8);
261 * Use the tbl24_index to access the required tbl24 entry then check if
262 * the tbl24 entry is INVALID, if so return -ENOENT.
264 if (!lpm->tbl24[tbl24_index].valid){
265 return -ENOENT; /* Lookup miss. */
268 * If tbl24 entry is valid check if it is NOT extended (i.e. it does
269 * not use a tbl8 extension) if so return the next hop.
271 if (likely(lpm->tbl24[tbl24_index].ext_entry == 0)) {
272 *next_hop = lpm->tbl24[tbl24_index].next_hop;
273 return 0; /* Lookup hit. */
277 * If tbl24 entry is valid and extended calculate the index into the
280 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
281 tbl8_index = (tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
284 /* Check if the tbl8 entry is invalid and if so return -ENOENT. */
285 if (!lpm->tbl8[tbl8_index].valid)
286 return -ENOENT;/* Lookup miss. */
288 /* If the tbl8 entry is valid return return the next_hop. */
289 *next_hop = lpm->tbl8[tbl8_index].next_hop;
290 return 0; /* Lookup hit. */
297 #endif /* _RTE_LPM_H_ */