update Intel copyright years to 2014
[dpdk.git] / lib / librte_lpm / rte_lpm.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 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 #ifndef _RTE_LPM_H_
35 #define _RTE_LPM_H_
36
37 /**
38  * @file
39  * RTE Longest Prefix Match (LPM)
40  */
41
42 #include <errno.h>
43 #include <sys/queue.h>
44 #include <stdint.h>
45 #include <stdlib.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_memory.h>
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 /** Max number of characters in LPM name. */
54 #define RTE_LPM_NAMESIZE                32
55
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
60
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
65
66 /** Maximum depth value possible for IPv4 LPM. */
67 #define RTE_LPM_MAX_DEPTH               32
68
69 /** @internal Total number of tbl24 entries. */
70 #define RTE_LPM_TBL24_NUM_ENTRIES       (1 << 24)
71
72 /** @internal Number of entries in a tbl8 group. */
73 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES  256
74
75 /** @internal Total number of tbl8 groups in the tbl8. */
76 #define RTE_LPM_TBL8_NUM_GROUPS         256
77
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)
81
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);                \
86 } while (0)
87 #else
88 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
89 #endif
90
91 /** @internal bitmask with valid and ext_entry/valid_group fields set */
92 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x0300
93
94 /** Bitmask used to indicate successful lookup */
95 #define RTE_LPM_LOOKUP_SUCCESS          0x0100
96
97 /** @internal Tbl24 entry structure. */
98 struct rte_lpm_tbl24_entry {
99         /* Stores Next hop or group index (i.e. gindex)into tbl8. */
100         union {
101                 uint8_t next_hop;
102                 uint8_t tbl8_gindex;
103         };
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. */
108 };
109
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. */
117 };
118
119 /** @internal Rule structure. */
120 struct rte_lpm_rule {
121         uint32_t ip; /**< Rule IP address. */
122         uint8_t  next_hop; /**< Rule next hop. */
123 };
124
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. */
129 };
130
131 /** @internal LPM structure. */
132 struct rte_lpm {
133         TAILQ_ENTRY(rte_lpm) next;      /**< Next in list. */
134
135         /* LPM metadata. */
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. */
140
141         /* LPM Tables. */
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. */
148 };
149
150 /**
151  * Create an LPM object.
152  *
153  * @param name
154  *   LPM object name
155  * @param socket_id
156  *   NUMA socket ID for LPM table memory allocation
157  * @param max_rules
158  *   Maximum number of LPM rules that can be added
159  * @param flags
160  *   This parameter is currently unused
161  * @return
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
171  */
172 struct rte_lpm *
173 rte_lpm_create(const char *name, int socket_id, int max_rules, int flags);
174
175 /**
176  * Find an existing LPM object and return a pointer to it.
177  *
178  * @param name
179  *   Name of the lpm object as passed to rte_lpm_create()
180  * @return
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.
184  */
185 struct rte_lpm *
186 rte_lpm_find_existing(const char *name);
187
188 /**
189  * Free an LPM object.
190  *
191  * @param lpm
192  *   LPM object handle
193  * @return
194  *   None
195  */
196 void
197 rte_lpm_free(struct rte_lpm *lpm);
198
199 /**
200  * Add a rule to the LPM table.
201  *
202  * @param lpm
203  *   LPM object handle
204  * @param ip
205  *   IP of the rule to be added to the LPM table
206  * @param depth
207  *   Depth of the rule to be added to the LPM table
208  * @param next_hop
209  *   Next hop of the rule to be added to the LPM table
210  * @return
211  *   0 on success, negative value otherwise
212  */
213 int
214 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t next_hop);
215
216 /**
217  * Delete a rule from the LPM table.
218  *
219  * @param lpm
220  *   LPM object handle
221  * @param ip
222  *   IP of the rule to be deleted from the LPM table
223  * @param depth
224  *   Depth of the rule to be deleted from the LPM table
225  * @return
226  *   0 on success, negative value otherwise
227  */
228 int
229 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
230
231 /**
232  * Delete all rules from the LPM table.
233  *
234  * @param lpm
235  *   LPM object handle
236  */
237 void
238 rte_lpm_delete_all(struct rte_lpm *lpm);
239
240 /**
241  * Lookup an IP into the LPM table.
242  *
243  * @param lpm
244  *   LPM object handle
245  * @param ip
246  *   IP to be looked up in the LPM table
247  * @param next_hop
248  *   Next hop of the most specific rule found for IP (valid on lookup hit only)
249  * @return
250  *   -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
251  */
252 static inline int
253 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
254 {
255         unsigned tbl24_index = (ip >> 8);
256         uint16_t tbl_entry;
257
258         /* DEBUG: Check user input arguments. */
259         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
260
261         /* Copy tbl24 entry */
262         tbl_entry = *(const uint16_t *)&lpm->tbl24[tbl24_index];
263
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)) {
267
268                 unsigned tbl8_index = (uint8_t)ip +
269                                 ((uint8_t)tbl_entry * RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
270
271                 tbl_entry = *(const uint16_t *)&lpm->tbl8[tbl8_index];
272         }
273
274         *next_hop = (uint8_t)tbl_entry;
275         return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
276 }
277
278 /**
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.
281  *
282  * @param lpm
283  *   LPM object handle
284  * @param ips
285  *   Array of IPs to be looked up in the LPM table
286  * @param next_hops
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
291  *   actual next hop.
292  * @param n
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.
295  *  @return
296  *   -EINVAL for incorrect arguments, otherwise 0
297  */
298 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
299                 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
300
301 static inline int
302 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t * ips,
303                 uint16_t * next_hops, const unsigned n)
304 {
305         unsigned i;
306         unsigned tbl24_indexes[n];
307
308         /* DEBUG: Check user input arguments. */
309         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
310                         (next_hops == NULL)), -EINVAL);
311
312         for (i = 0; i < n; i++) {
313                 tbl24_indexes[i] = ips[i] >> 8;
314         }
315
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]];
319
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)) {
323
324                         unsigned tbl8_index = (uint8_t)ips[i] +
325                                         ((uint8_t)next_hops[i] *
326                                          RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
327
328                         next_hops[i] = *(const uint16_t *)&lpm->tbl8[tbl8_index];
329                 }
330         }
331         return 0;
332 }
333
334 #ifdef __cplusplus
335 }
336 #endif
337
338 #endif /* _RTE_LPM_H_ */