b9e03b13a620cbe1e340886410d1e3f96461b853
[dpdk.git] / lib / librte_lpm / rte_lpm.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  */
34
35 #ifndef _RTE_LPM_H_
36 #define _RTE_LPM_H_
37
38 /**
39  * @file
40  * RTE Longest Prefix Match (LPM)
41  */
42
43 #include <errno.h>
44 #include <sys/queue.h>
45 #include <stdint.h>
46 #include <stdlib.h>
47 #include <rte_branch_prediction.h>
48 #include <rte_memory.h>
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54 /** Max number of characters in LPM name. */
55 #define RTE_LPM_NAMESIZE                32
56
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
61
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
66
67 /** Maximum depth value possible for IPv4 LPM. */
68 #define RTE_LPM_MAX_DEPTH               32
69
70 /** @internal Total number of tbl24 entries. */
71 #define RTE_LPM_TBL24_NUM_ENTRIES       (1 << 24)
72
73 /** @internal Number of entries in a tbl8 group. */
74 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES  256
75
76 /** @internal Total number of tbl8 groups in the tbl8. */
77 #define RTE_LPM_TBL8_NUM_GROUPS         256
78
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)
82
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);                \
87 } while (0)
88 #else
89 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
90 #endif
91
92 /** @internal bitmask with valid and ext_entry/valid_group fields set */
93 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x0300
94
95 /** Bitmask used to indicate successful lookup */
96 #define RTE_LPM_LOOKUP_SUCCESS          0x0100
97
98 /** @internal Tbl24 entry structure. */
99 struct rte_lpm_tbl24_entry {
100         /* Stores Next hop or group index (i.e. gindex)into tbl8. */
101         union {
102                 uint8_t next_hop;
103                 uint8_t tbl8_gindex;
104         };
105         /* Using single uint8_t to store 3 values. */
106         uint8_t valid     :1; /**< Validation flag. */
107         uint8_t ext_entry :1; /**< External entry. */
108         uint8_t depth     :6; /**< Rule depth. */
109 };
110
111 /** @internal Tbl8 entry structure. */
112 struct rte_lpm_tbl8_entry {
113         uint8_t next_hop; /**< next hop. */
114         /* Using single uint8_t to store 3 values. */
115         uint8_t valid       :1; /**< Validation flag. */
116         uint8_t valid_group :1; /**< Group validation flag. */
117         uint8_t depth       :6; /**< Rule depth. */
118 };
119
120 /** @internal Rule structure. */
121 struct rte_lpm_rule {
122         uint32_t ip; /**< Rule IP address. */
123         uint8_t  next_hop; /**< Rule next hop. */
124 };
125
126 /** @internal LPM structure. */
127 struct rte_lpm {
128         TAILQ_ENTRY(rte_lpm) next;      /**< Next in list. */
129
130         /* LPM metadata. */
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. */
135
136         /* LPM Tables. */
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. */
143 };
144
145 /**
146  * Create an LPM object.
147  *
148  * @param name
149  *   LPM object name
150  * @param socket_id
151  *   NUMA socket ID for LPM table memory allocation
152  * @param max_rules
153  *   Maximum number of LPM rules that can be added
154  * @param flags
155  *   This parameter is currently unused
156  * @return
157  *   Handle to LPM object on success, NULL otherwise with rte_errno set
158  *   to an appropriate values. Possible rte_errno values include:
159  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
160  *    - E_RTE_SECONDARY - function was called from a secondary process instance
161  *    - E_RTE_NO_TAILQ - no tailq list could be got for the lpm object list
162  *    - EINVAL - invalid parameter passed to function
163  *    - ENOSPC - the maximum number of memzones has already been allocated
164  *    - EEXIST - a memzone with the same name already exists
165  *    - ENOMEM - no appropriate memory area found in which to create memzone
166  */
167 struct rte_lpm *
168 rte_lpm_create(const char *name, int socket_id, int max_rules, int flags);
169
170 /**
171  * Find an existing LPM object and return a pointer to it.
172  *
173  * @param name
174  *   Name of the lpm object as passed to rte_lpm_create()
175  * @return
176  *   Pointer to lpm object or NULL if object not found with rte_errno
177  *   set appropriately. Possible rte_errno values include:
178  *    - ENOENT - required entry not available to return.
179  */
180 struct rte_lpm *
181 rte_lpm_find_existing(const char *name);
182
183 /**
184  * Free an LPM object.
185  *
186  * @param lpm
187  *   LPM object handle
188  * @return
189  *   None
190  */
191 void
192 rte_lpm_free(struct rte_lpm *lpm);
193
194 /**
195  * Add a rule to the LPM table.
196  *
197  * @param lpm
198  *   LPM object handle
199  * @param ip
200  *   IP of the rule to be added to the LPM table
201  * @param depth
202  *   Depth of the rule to be added to the LPM table
203  * @param next_hop
204  *   Next hop of the rule to be added to the LPM table
205  * @return
206  *   0 on success, negative value otherwise
207  */
208 int
209 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t next_hop);
210
211 /**
212  * Delete a rule from the LPM table.
213  *
214  * @param lpm
215  *   LPM object handle
216  * @param ip
217  *   IP of the rule to be deleted from the LPM table
218  * @param depth
219  *   Depth of the rule to be deleted from the LPM table
220  * @return
221  *   0 on success, negative value otherwise
222  */
223 int
224 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
225
226 /**
227  * Delete all rules from the LPM table.
228  *
229  * @param lpm
230  *   LPM object handle
231  */
232 void
233 rte_lpm_delete_all(struct rte_lpm *lpm);
234
235 /**
236  * Lookup an IP into the LPM table.
237  *
238  * @param lpm
239  *   LPM object handle
240  * @param ip
241  *   IP to be looked up in the LPM table
242  * @param next_hop
243  *   Next hop of the most specific rule found for IP (valid on lookup hit only)
244  * @return
245  *   -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
246  */
247 static inline int
248 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
249 {
250         unsigned tbl24_index = (ip >> 8);
251         uint16_t tbl_entry;
252
253         /* DEBUG: Check user input arguments. */
254         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
255
256         /* Copy tbl24 entry */
257         tbl_entry = *(const uint16_t *)&lpm->tbl24[tbl24_index];
258
259         /* Copy tbl8 entry (only if needed) */
260         if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
261                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
262
263                 unsigned tbl8_index = (uint8_t)ip +
264                                 ((uint8_t)tbl_entry * RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
265
266                 tbl_entry = *(const uint16_t *)&lpm->tbl8[tbl8_index];
267         }
268
269         *next_hop = (uint8_t)tbl_entry;
270         return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
271 }
272
273 /**
274  * Lookup multiple IP addresses in an LPM table. This may be implemented as a
275  * macro, so the address of the function should not be used.
276  *
277  * @param lpm
278  *   LPM object handle
279  * @param ips
280  *   Array of IPs to be looked up in the LPM table
281  * @param next_hops
282  *   Next hop of the most specific rule found for IP (valid on lookup hit only).
283  *   This is an array of two byte values. The most significant byte in each
284  *   value says whether the lookup was successful (bitmask
285  *   RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
286  *   actual next hop.
287  * @param n
288  *   Number of elements in ips (and next_hops) array to lookup. This should be a
289  *   compile time constant, and divisible by 8 for best performance.
290  *  @return
291  *   -EINVAL for incorrect arguments, otherwise 0
292  */
293 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
294                 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
295
296 static inline int
297 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t * ips,
298                 uint16_t * next_hops, const unsigned n)
299 {
300         unsigned i;
301         unsigned tbl24_indexes[n];
302
303         /* DEBUG: Check user input arguments. */
304         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
305                         (next_hops == NULL)), -EINVAL);
306
307         for (i = 0; i < n; i++) {
308                 tbl24_indexes[i] = ips[i] >> 8;
309         }
310
311         for (i = 0; i < n; i++) {
312                 /* Simply copy tbl24 entry to output */
313                 next_hops[i] = *(const uint16_t *)&lpm->tbl24[tbl24_indexes[i]];
314
315                 /* Overwrite output with tbl8 entry if needed */
316                 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
317                                 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
318
319                         unsigned tbl8_index = (uint8_t)ips[i] +
320                                         ((uint8_t)next_hops[i] *
321                                          RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
322
323                         next_hops[i] = *(const uint16_t *)&lpm->tbl8[tbl8_index];
324                 }
325         }
326         return 0;
327 }
328
329 #ifdef __cplusplus
330 }
331 #endif
332
333 #endif /* _RTE_LPM_H_ */