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