Imported Upstream version 16.04
[deb_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_byteorder.h>
48 #include <rte_memory.h>
49 #include <rte_common.h>
50 #include <rte_vect.h>
51 #include <rte_compat.h>
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57 /** Max number of characters in LPM name. */
58 #define RTE_LPM_NAMESIZE                32
59
60 /** Maximum depth value possible for IPv4 LPM. */
61 #define RTE_LPM_MAX_DEPTH               32
62
63 /** @internal Total number of tbl24 entries. */
64 #define RTE_LPM_TBL24_NUM_ENTRIES       (1 << 24)
65
66 /** @internal Number of entries in a tbl8 group. */
67 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES  256
68
69 /** @internal Max number of tbl8 groups in the tbl8. */
70 #define RTE_LPM_MAX_TBL8_NUM_GROUPS         (1 << 24)
71
72 /** @internal Total number of tbl8 groups in the tbl8. */
73 #define RTE_LPM_TBL8_NUM_GROUPS         256
74
75 /** @internal Total number of tbl8 entries. */
76 #define RTE_LPM_TBL8_NUM_ENTRIES        (RTE_LPM_TBL8_NUM_GROUPS * \
77                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
78
79 /** @internal Macro to enable/disable run-time checks. */
80 #if defined(RTE_LIBRTE_LPM_DEBUG)
81 #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
82         if (cond) return (retval);                \
83 } while (0)
84 #else
85 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
86 #endif
87
88 /** @internal bitmask with valid and valid_group fields set */
89 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x03000000
90
91 /** Bitmask used to indicate successful lookup */
92 #define RTE_LPM_LOOKUP_SUCCESS          0x01000000
93
94 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
95 /** @internal Tbl24 entry structure. */
96 struct rte_lpm_tbl_entry_v20 {
97         /**
98          * Stores Next hop (tbl8 or tbl24 when valid_group is not set) or
99          * a group index pointing to a tbl8 structure (tbl24 only, when
100          * valid_group is set)
101          */
102         union {
103                 uint8_t next_hop;
104                 uint8_t group_idx;
105         };
106         /* Using single uint8_t to store 3 values. */
107         uint8_t valid     :1;   /**< Validation flag. */
108         /**
109          * For tbl24:
110          *  - valid_group == 0: entry stores a next hop
111          *  - valid_group == 1: entry stores a group_index pointing to a tbl8
112          * For tbl8:
113          *  - valid_group indicates whether the current tbl8 is in use or not
114          */
115         uint8_t valid_group :1;
116         uint8_t depth       :6; /**< Rule depth. */
117 };
118
119 struct rte_lpm_tbl_entry {
120         /**
121          * Stores Next hop (tbl8 or tbl24 when valid_group is not set) or
122          * a group index pointing to a tbl8 structure (tbl24 only, when
123          * valid_group is set)
124          */
125         uint32_t next_hop    :24;
126         /* Using single uint8_t to store 3 values. */
127         uint32_t valid       :1;   /**< Validation flag. */
128         /**
129          * For tbl24:
130          *  - valid_group == 0: entry stores a next hop
131          *  - valid_group == 1: entry stores a group_index pointing to a tbl8
132          * For tbl8:
133          *  - valid_group indicates whether the current tbl8 is in use or not
134          */
135         uint32_t valid_group :1;
136         uint32_t depth       :6; /**< Rule depth. */
137 };
138
139 #else
140 struct rte_lpm_tbl_entry_v20 {
141         uint8_t depth       :6;
142         uint8_t valid_group :1;
143         uint8_t valid       :1;
144         union {
145                 uint8_t group_idx;
146                 uint8_t next_hop;
147         };
148 };
149
150 struct rte_lpm_tbl_entry {
151         uint32_t depth       :6;
152         uint32_t valid_group :1;
153         uint32_t valid       :1;
154         uint32_t next_hop    :24;
155
156 };
157
158 #endif
159
160 /** LPM configuration structure. */
161 struct rte_lpm_config {
162         uint32_t max_rules;      /**< Max number of rules. */
163         uint32_t number_tbl8s;   /**< Number of tbl8s to allocate. */
164         int flags;               /**< This field is currently unused. */
165 };
166
167 /** @internal Rule structure. */
168 struct rte_lpm_rule_v20 {
169         uint32_t ip; /**< Rule IP address. */
170         uint8_t  next_hop; /**< Rule next hop. */
171 };
172
173 struct rte_lpm_rule {
174         uint32_t ip; /**< Rule IP address. */
175         uint32_t next_hop; /**< Rule next hop. */
176 };
177
178 /** @internal Contains metadata about the rules table. */
179 struct rte_lpm_rule_info {
180         uint32_t used_rules; /**< Used rules so far. */
181         uint32_t first_rule; /**< Indexes the first rule of a given depth. */
182 };
183
184 /** @internal LPM structure. */
185 struct rte_lpm_v20 {
186         /* LPM metadata. */
187         char name[RTE_LPM_NAMESIZE];        /**< Name of the lpm. */
188         uint32_t max_rules; /**< Max. balanced rules per lpm. */
189         struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH]; /**< Rule info table. */
190
191         /* LPM Tables. */
192         struct rte_lpm_tbl_entry_v20 tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
193                         __rte_cache_aligned; /**< LPM tbl24 table. */
194         struct rte_lpm_tbl_entry_v20 tbl8[RTE_LPM_TBL8_NUM_ENTRIES]
195                         __rte_cache_aligned; /**< LPM tbl8 table. */
196         struct rte_lpm_rule_v20 rules_tbl[0] \
197                         __rte_cache_aligned; /**< LPM rules. */
198 };
199
200 struct rte_lpm {
201         /* LPM metadata. */
202         char name[RTE_LPM_NAMESIZE];        /**< Name of the lpm. */
203         uint32_t max_rules; /**< Max. balanced rules per lpm. */
204         uint32_t number_tbl8s; /**< Number of tbl8s. */
205         struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH]; /**< Rule info table. */
206
207         /* LPM Tables. */
208         struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
209                         __rte_cache_aligned; /**< LPM tbl24 table. */
210         struct rte_lpm_tbl_entry *tbl8; /**< LPM tbl8 table. */
211         struct rte_lpm_rule *rules_tbl; /**< LPM rules. */
212 };
213
214 /**
215  * Create an LPM object.
216  *
217  * @param name
218  *   LPM object name
219  * @param socket_id
220  *   NUMA socket ID for LPM table memory allocation
221  * @param config
222  *   Structure containing the configuration
223  * @return
224  *   Handle to LPM object on success, NULL otherwise with rte_errno set
225  *   to an appropriate values. Possible rte_errno values include:
226  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
227  *    - E_RTE_SECONDARY - function was called from a secondary process instance
228  *    - EINVAL - invalid parameter passed to function
229  *    - ENOSPC - the maximum number of memzones has already been allocated
230  *    - EEXIST - a memzone with the same name already exists
231  *    - ENOMEM - no appropriate memory area found in which to create memzone
232  */
233 struct rte_lpm *
234 rte_lpm_create(const char *name, int socket_id,
235                 const struct rte_lpm_config *config);
236 struct rte_lpm_v20 *
237 rte_lpm_create_v20(const char *name, int socket_id, int max_rules, int flags);
238 struct rte_lpm *
239 rte_lpm_create_v1604(const char *name, int socket_id,
240                 const struct rte_lpm_config *config);
241
242 /**
243  * Find an existing LPM object and return a pointer to it.
244  *
245  * @param name
246  *   Name of the lpm object as passed to rte_lpm_create()
247  * @return
248  *   Pointer to lpm object or NULL if object not found with rte_errno
249  *   set appropriately. Possible rte_errno values include:
250  *    - ENOENT - required entry not available to return.
251  */
252 struct rte_lpm *
253 rte_lpm_find_existing(const char *name);
254 struct rte_lpm_v20 *
255 rte_lpm_find_existing_v20(const char *name);
256 struct rte_lpm *
257 rte_lpm_find_existing_v1604(const char *name);
258
259 /**
260  * Free an LPM object.
261  *
262  * @param lpm
263  *   LPM object handle
264  * @return
265  *   None
266  */
267 void
268 rte_lpm_free(struct rte_lpm *lpm);
269 void
270 rte_lpm_free_v20(struct rte_lpm_v20 *lpm);
271 void
272 rte_lpm_free_v1604(struct rte_lpm *lpm);
273
274 /**
275  * Add a rule to the LPM table.
276  *
277  * @param lpm
278  *   LPM object handle
279  * @param ip
280  *   IP of the rule to be added to the LPM table
281  * @param depth
282  *   Depth of the rule to be added to the LPM table
283  * @param next_hop
284  *   Next hop of the rule to be added to the LPM table
285  * @return
286  *   0 on success, negative value otherwise
287  */
288 int
289 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
290 int
291 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
292                 uint8_t next_hop);
293 int
294 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
295                 uint32_t next_hop);
296
297 /**
298  * Check if a rule is present in the LPM table,
299  * and provide its next hop if it is.
300  *
301  * @param lpm
302  *   LPM object handle
303  * @param ip
304  *   IP of the rule to be searched
305  * @param depth
306  *   Depth of the rule to searched
307  * @param next_hop
308  *   Next hop of the rule (valid only if it is found)
309  * @return
310  *   1 if the rule exists, 0 if it does not, a negative value on failure
311  */
312 int
313 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
314 uint32_t *next_hop);
315 int
316 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
317 uint8_t *next_hop);
318 int
319 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
320 uint32_t *next_hop);
321
322 /**
323  * Delete a rule from the LPM table.
324  *
325  * @param lpm
326  *   LPM object handle
327  * @param ip
328  *   IP of the rule to be deleted from the LPM table
329  * @param depth
330  *   Depth of the rule to be deleted from the LPM table
331  * @return
332  *   0 on success, negative value otherwise
333  */
334 int
335 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
336 int
337 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth);
338 int
339 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
340
341 /**
342  * Delete all rules from the LPM table.
343  *
344  * @param lpm
345  *   LPM object handle
346  */
347 void
348 rte_lpm_delete_all(struct rte_lpm *lpm);
349 void
350 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm);
351 void
352 rte_lpm_delete_all_v1604(struct rte_lpm *lpm);
353
354 /**
355  * Lookup an IP into the LPM table.
356  *
357  * @param lpm
358  *   LPM object handle
359  * @param ip
360  *   IP to be looked up in the LPM table
361  * @param next_hop
362  *   Next hop of the most specific rule found for IP (valid on lookup hit only)
363  * @return
364  *   -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
365  */
366 static inline int
367 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
368 {
369         unsigned tbl24_index = (ip >> 8);
370         uint32_t tbl_entry;
371         const uint32_t *ptbl;
372
373         /* DEBUG: Check user input arguments. */
374         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
375
376         /* Copy tbl24 entry */
377         ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
378         tbl_entry = *ptbl;
379
380         /* Copy tbl8 entry (only if needed) */
381         if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
382                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
383
384                 unsigned tbl8_index = (uint8_t)ip +
385                                 (((uint32_t)tbl_entry & 0x00FFFFFF) *
386                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
387
388                 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
389                 tbl_entry = *ptbl;
390         }
391
392         *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
393         return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
394 }
395
396 /**
397  * Lookup multiple IP addresses in an LPM table. This may be implemented as a
398  * macro, so the address of the function should not be used.
399  *
400  * @param lpm
401  *   LPM object handle
402  * @param ips
403  *   Array of IPs to be looked up in the LPM table
404  * @param next_hops
405  *   Next hop of the most specific rule found for IP (valid on lookup hit only).
406  *   This is an array of two byte values. The most significant byte in each
407  *   value says whether the lookup was successful (bitmask
408  *   RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
409  *   actual next hop.
410  * @param n
411  *   Number of elements in ips (and next_hops) array to lookup. This should be a
412  *   compile time constant, and divisible by 8 for best performance.
413  *  @return
414  *   -EINVAL for incorrect arguments, otherwise 0
415  */
416 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
417                 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
418
419 static inline int
420 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
421                 uint32_t *next_hops, const unsigned n)
422 {
423         unsigned i;
424         unsigned tbl24_indexes[n];
425         const uint32_t *ptbl;
426
427         /* DEBUG: Check user input arguments. */
428         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
429                         (next_hops == NULL)), -EINVAL);
430
431         for (i = 0; i < n; i++) {
432                 tbl24_indexes[i] = ips[i] >> 8;
433         }
434
435         for (i = 0; i < n; i++) {
436                 /* Simply copy tbl24 entry to output */
437                 ptbl = (const uint32_t *)&lpm->tbl24[tbl24_indexes[i]];
438                 next_hops[i] = *ptbl;
439
440                 /* Overwrite output with tbl8 entry if needed */
441                 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
442                                 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
443
444                         unsigned tbl8_index = (uint8_t)ips[i] +
445                                         (((uint32_t)next_hops[i] & 0x00FFFFFF) *
446                                          RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
447
448                         ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
449                         next_hops[i] = *ptbl;
450                 }
451         }
452         return 0;
453 }
454
455 /* Mask four results. */
456 #define  RTE_LPM_MASKX4_RES     UINT64_C(0x00ffffff00ffffff)
457
458 /**
459  * Lookup four IP addresses in an LPM table.
460  *
461  * @param lpm
462  *   LPM object handle
463  * @param ip
464  *   Four IPs to be looked up in the LPM table
465  * @param hop
466  *   Next hop of the most specific rule found for IP (valid on lookup hit only).
467  *   This is an 4 elements array of two byte values.
468  *   If the lookup was succesfull for the given IP, then least significant byte
469  *   of the corresponding element is the  actual next hop and the most
470  *   significant byte is zero.
471  *   If the lookup for the given IP failed, then corresponding element would
472  *   contain default value, see description of then next parameter.
473  * @param defv
474  *   Default value to populate into corresponding element of hop[] array,
475  *   if lookup would fail.
476  */
477 static inline void
478 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
479         uint32_t defv);
480
481 #if defined(RTE_ARCH_ARM) || defined(RTE_ARCH_ARM64)
482 #include "rte_lpm_neon.h"
483 #else
484 #include "rte_lpm_sse.h"
485 #endif
486
487 #ifdef __cplusplus
488 }
489 #endif
490
491 #endif /* _RTE_LPM_H_ */