doc: whitespace changes in licenses
[dpdk.git] / lib / librte_lpm / rte_lpm.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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 LPM structure. */
126 struct rte_lpm {
127         TAILQ_ENTRY(rte_lpm) next;      /**< Next in list. */
128
129         /* LPM metadata. */
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. */
134
135         /* LPM Tables. */
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. */
142 };
143
144 /**
145  * Create an LPM object.
146  *
147  * @param name
148  *   LPM object name
149  * @param socket_id
150  *   NUMA socket ID for LPM table memory allocation
151  * @param max_rules
152  *   Maximum number of LPM rules that can be added
153  * @param flags
154  *   This parameter is currently unused
155  * @return
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
165  */
166 struct rte_lpm *
167 rte_lpm_create(const char *name, int socket_id, int max_rules, int flags);
168
169 /**
170  * Find an existing LPM object and return a pointer to it.
171  *
172  * @param name
173  *   Name of the lpm object as passed to rte_lpm_create()
174  * @return
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.
178  */
179 struct rte_lpm *
180 rte_lpm_find_existing(const char *name);
181
182 /**
183  * Free an LPM object.
184  *
185  * @param lpm
186  *   LPM object handle
187  * @return
188  *   None
189  */
190 void
191 rte_lpm_free(struct rte_lpm *lpm);
192
193 /**
194  * Add a rule to the LPM table.
195  *
196  * @param lpm
197  *   LPM object handle
198  * @param ip
199  *   IP of the rule to be added to the LPM table
200  * @param depth
201  *   Depth of the rule to be added to the LPM table
202  * @param next_hop
203  *   Next hop of the rule to be added to the LPM table
204  * @return
205  *   0 on success, negative value otherwise
206  */
207 int
208 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t next_hop);
209
210 /**
211  * Delete a rule from the LPM table.
212  *
213  * @param lpm
214  *   LPM object handle
215  * @param ip
216  *   IP of the rule to be deleted from the LPM table
217  * @param depth
218  *   Depth of the rule to be deleted from the LPM table
219  * @return
220  *   0 on success, negative value otherwise
221  */
222 int
223 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
224
225 /**
226  * Delete all rules from the LPM table.
227  *
228  * @param lpm
229  *   LPM object handle
230  */
231 void
232 rte_lpm_delete_all(struct rte_lpm *lpm);
233
234 /**
235  * Lookup an IP into the LPM table.
236  *
237  * @param lpm
238  *   LPM object handle
239  * @param ip
240  *   IP to be looked up in the LPM table
241  * @param next_hop
242  *   Next hop of the most specific rule found for IP (valid on lookup hit only)
243  * @return
244  *   -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
245  */
246 static inline int
247 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
248 {
249         unsigned tbl24_index = (ip >> 8);
250         uint16_t tbl_entry;
251
252         /* DEBUG: Check user input arguments. */
253         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
254
255         /* Copy tbl24 entry */
256         tbl_entry = *(const uint16_t *)&lpm->tbl24[tbl24_index];
257
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)) {
261
262                 unsigned tbl8_index = (uint8_t)ip +
263                                 ((uint8_t)tbl_entry * RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
264
265                 tbl_entry = *(const uint16_t *)&lpm->tbl8[tbl8_index];
266         }
267
268         *next_hop = (uint8_t)tbl_entry;
269         return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
270 }
271
272 /**
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.
275  *
276  * @param lpm
277  *   LPM object handle
278  * @param ips
279  *   Array of IPs to be looked up in the LPM table
280  * @param next_hops
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
285  *   actual next hop.
286  * @param n
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.
289  *  @return
290  *   -EINVAL for incorrect arguments, otherwise 0
291  */
292 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
293                 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
294
295 static inline int
296 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t * ips,
297                 uint16_t * next_hops, const unsigned n)
298 {
299         unsigned i;
300         unsigned tbl24_indexes[n];
301
302         /* DEBUG: Check user input arguments. */
303         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
304                         (next_hops == NULL)), -EINVAL);
305
306         for (i = 0; i < n; i++) {
307                 tbl24_indexes[i] = ips[i] >> 8;
308         }
309
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]];
313
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)) {
317
318                         unsigned tbl8_index = (uint8_t)ips[i] +
319                                         ((uint8_t)next_hops[i] *
320                                          RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
321
322                         next_hops[i] = *(const uint16_t *)&lpm->tbl8[tbl8_index];
323                 }
324         }
325         return 0;
326 }
327
328 #ifdef __cplusplus
329 }
330 #endif
331
332 #endif /* _RTE_LPM_H_ */