eal: introduce ymm type for AVX 256-bit
[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 #include <rte_common.h>
49 #include <rte_common_vect.h>
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 /** Max number of characters in LPM name. */
56 #define RTE_LPM_NAMESIZE                32
57
58 /** @deprecated Possible location to allocate memory. This was for last
59  * parameter of rte_lpm_create(), but is now redundant. The LPM table is always
60  * allocated in memory using librte_malloc which uses a memzone. */
61 #define RTE_LPM_HEAP                    0
62
63 /** @deprecated Possible location to allocate memory. This was for last
64  * parameter of rte_lpm_create(), but is now redundant. The LPM table is always
65  * allocated in memory using librte_malloc which uses a memzone. */
66 #define RTE_LPM_MEMZONE                 1
67
68 /** Maximum depth value possible for IPv4 LPM. */
69 #define RTE_LPM_MAX_DEPTH               32
70
71 /** @internal Total number of tbl24 entries. */
72 #define RTE_LPM_TBL24_NUM_ENTRIES       (1 << 24)
73
74 /** @internal Number of entries in a tbl8 group. */
75 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES  256
76
77 /** @internal Total number of tbl8 groups in the tbl8. */
78 #define RTE_LPM_TBL8_NUM_GROUPS         256
79
80 /** @internal Total number of tbl8 entries. */
81 #define RTE_LPM_TBL8_NUM_ENTRIES        (RTE_LPM_TBL8_NUM_GROUPS * \
82                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
83
84 /** @internal Macro to enable/disable run-time checks. */
85 #if defined(RTE_LIBRTE_LPM_DEBUG)
86 #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
87         if (cond) return (retval);                \
88 } while (0)
89 #else
90 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
91 #endif
92
93 /** @internal bitmask with valid and ext_entry/valid_group fields set */
94 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x0300
95
96 /** Bitmask used to indicate successful lookup */
97 #define RTE_LPM_LOOKUP_SUCCESS          0x0100
98
99 /** @internal Tbl24 entry structure. */
100 struct rte_lpm_tbl24_entry {
101         /* Stores Next hop or group index (i.e. gindex)into tbl8. */
102         union {
103                 uint8_t next_hop;
104                 uint8_t tbl8_gindex;
105         };
106         /* Using single uint8_t to store 3 values. */
107         uint8_t valid     :1; /**< Validation flag. */
108         uint8_t ext_entry :1; /**< External entry. */
109         uint8_t depth     :6; /**< Rule depth. */
110 };
111
112 /** @internal Tbl8 entry structure. */
113 struct rte_lpm_tbl8_entry {
114         uint8_t next_hop; /**< next hop. */
115         /* Using single uint8_t to store 3 values. */
116         uint8_t valid       :1; /**< Validation flag. */
117         uint8_t valid_group :1; /**< Group validation flag. */
118         uint8_t depth       :6; /**< Rule depth. */
119 };
120
121 /** @internal Rule structure. */
122 struct rte_lpm_rule {
123         uint32_t ip; /**< Rule IP address. */
124         uint8_t  next_hop; /**< Rule next hop. */
125 };
126
127 /** @internal Contains metadata about the rules table. */
128 struct rte_lpm_rule_info {
129         uint32_t used_rules; /**< Used rules so far. */
130         uint32_t first_rule; /**< Indexes the first rule of a given depth. */
131 };
132
133 /** @internal LPM structure. */
134 struct rte_lpm {
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  * Check if a rule is present in the LPM table,
218  * and provide its next hop if it is.
219  *
220  * @param lpm
221  *   LPM object handle
222  * @param ip
223  *   IP of the rule to be searched
224  * @param depth
225  *   Depth of the rule to searched
226  * @param next_hop
227  *   Next hop of the rule (valid only if it is found)
228  * @return
229  *   1 if the rule exists, 0 if it does not, a negative value on failure
230  */
231 int
232 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
233 uint8_t *next_hop);
234
235 /**
236  * Delete a rule from the LPM table.
237  *
238  * @param lpm
239  *   LPM object handle
240  * @param ip
241  *   IP of the rule to be deleted from the LPM table
242  * @param depth
243  *   Depth of the rule to be deleted from the LPM table
244  * @return
245  *   0 on success, negative value otherwise
246  */
247 int
248 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
249
250 /**
251  * Delete all rules from the LPM table.
252  *
253  * @param lpm
254  *   LPM object handle
255  */
256 void
257 rte_lpm_delete_all(struct rte_lpm *lpm);
258
259 /**
260  * Lookup an IP into the LPM table.
261  *
262  * @param lpm
263  *   LPM object handle
264  * @param ip
265  *   IP to be looked up in the LPM table
266  * @param next_hop
267  *   Next hop of the most specific rule found for IP (valid on lookup hit only)
268  * @return
269  *   -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
270  */
271 static inline int
272 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
273 {
274         unsigned tbl24_index = (ip >> 8);
275         uint16_t tbl_entry;
276
277         /* DEBUG: Check user input arguments. */
278         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
279
280         /* Copy tbl24 entry */
281         tbl_entry = *(const uint16_t *)&lpm->tbl24[tbl24_index];
282
283         /* Copy tbl8 entry (only if needed) */
284         if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
285                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
286
287                 unsigned tbl8_index = (uint8_t)ip +
288                                 ((uint8_t)tbl_entry * RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
289
290                 tbl_entry = *(const uint16_t *)&lpm->tbl8[tbl8_index];
291         }
292
293         *next_hop = (uint8_t)tbl_entry;
294         return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
295 }
296
297 /**
298  * Lookup multiple IP addresses in an LPM table. This may be implemented as a
299  * macro, so the address of the function should not be used.
300  *
301  * @param lpm
302  *   LPM object handle
303  * @param ips
304  *   Array of IPs to be looked up in the LPM table
305  * @param next_hops
306  *   Next hop of the most specific rule found for IP (valid on lookup hit only).
307  *   This is an array of two byte values. The most significant byte in each
308  *   value says whether the lookup was successful (bitmask
309  *   RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
310  *   actual next hop.
311  * @param n
312  *   Number of elements in ips (and next_hops) array to lookup. This should be a
313  *   compile time constant, and divisible by 8 for best performance.
314  *  @return
315  *   -EINVAL for incorrect arguments, otherwise 0
316  */
317 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
318                 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
319
320 static inline int
321 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t * ips,
322                 uint16_t * next_hops, const unsigned n)
323 {
324         unsigned i;
325         unsigned tbl24_indexes[n];
326
327         /* DEBUG: Check user input arguments. */
328         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
329                         (next_hops == NULL)), -EINVAL);
330
331         for (i = 0; i < n; i++) {
332                 tbl24_indexes[i] = ips[i] >> 8;
333         }
334
335         for (i = 0; i < n; i++) {
336                 /* Simply copy tbl24 entry to output */
337                 next_hops[i] = *(const uint16_t *)&lpm->tbl24[tbl24_indexes[i]];
338
339                 /* Overwrite output with tbl8 entry if needed */
340                 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
341                                 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
342
343                         unsigned tbl8_index = (uint8_t)ips[i] +
344                                         ((uint8_t)next_hops[i] *
345                                          RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
346
347                         next_hops[i] = *(const uint16_t *)&lpm->tbl8[tbl8_index];
348                 }
349         }
350         return 0;
351 }
352
353 /* Mask four results. */
354 #define  RTE_LPM_MASKX4_RES     UINT64_C(0x00ff00ff00ff00ff)
355
356 /**
357  * Lookup four IP addresses in an LPM table.
358  *
359  * @param lpm
360  *   LPM object handle
361  * @param ip
362  *   Four IPs to be looked up in the LPM table
363  * @param hop
364  *   Next hop of the most specific rule found for IP (valid on lookup hit only).
365  *   This is an 4 elements array of two byte values.
366  *   If the lookup was succesfull for the given IP, then least significant byte
367  *   of the corresponding element is the  actual next hop and the most
368  *   significant byte is zero.
369  *   If the lookup for the given IP failed, then corresponding element would
370  *   contain default value, see description of then next parameter.
371  * @param defv
372  *   Default value to populate into corresponding element of hop[] array,
373  *   if lookup would fail.
374  */
375 static inline void
376 rte_lpm_lookupx4(const struct rte_lpm *lpm, __m128i ip, uint16_t hop[4],
377         uint16_t defv)
378 {
379         __m128i i24;
380         rte_xmm_t i8;
381         uint16_t tbl[4];
382         uint64_t idx, pt;
383
384         const __m128i mask8 =
385                 _mm_set_epi32(UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX);
386
387         /*
388          * RTE_LPM_VALID_EXT_ENTRY_BITMASK for 4 LPM entries
389          * as one 64-bit value (0x0300030003000300).
390          */
391         const uint64_t mask_xv =
392                 ((uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK |
393                 (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 16 |
394                 (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 32 |
395                 (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 48);
396
397         /*
398          * RTE_LPM_LOOKUP_SUCCESS for 4 LPM entries
399          * as one 64-bit value (0x0100010001000100).
400          */
401         const uint64_t mask_v =
402                 ((uint64_t)RTE_LPM_LOOKUP_SUCCESS |
403                 (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 16 |
404                 (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 32 |
405                 (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 48);
406
407         /* get 4 indexes for tbl24[]. */
408         i24 = _mm_srli_epi32(ip, CHAR_BIT);
409
410         /* extract values from tbl24[] */
411         idx = _mm_cvtsi128_si64(i24);
412         i24 = _mm_srli_si128(i24, sizeof(uint64_t));
413
414         tbl[0] = *(const uint16_t *)&lpm->tbl24[(uint32_t)idx];
415         tbl[1] = *(const uint16_t *)&lpm->tbl24[idx >> 32];
416
417         idx = _mm_cvtsi128_si64(i24);
418
419         tbl[2] = *(const uint16_t *)&lpm->tbl24[(uint32_t)idx];
420         tbl[3] = *(const uint16_t *)&lpm->tbl24[idx >> 32];
421
422         /* get 4 indexes for tbl8[]. */
423         i8.x = _mm_and_si128(ip, mask8);
424
425         pt = (uint64_t)tbl[0] |
426                 (uint64_t)tbl[1] << 16 |
427                 (uint64_t)tbl[2] << 32 |
428                 (uint64_t)tbl[3] << 48;
429
430         /* search successfully finished for all 4 IP addresses. */
431         if (likely((pt & mask_xv) == mask_v)) {
432                 uintptr_t ph = (uintptr_t)hop;
433                 *(uint64_t *)ph = pt & RTE_LPM_MASKX4_RES;
434                 return;
435         }
436
437         if (unlikely((pt & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
438                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
439                 i8.u32[0] = i8.u32[0] +
440                         (uint8_t)tbl[0] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
441                 tbl[0] = *(const uint16_t *)&lpm->tbl8[i8.u32[0]];
442         }
443         if (unlikely((pt >> 16 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
444                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
445                 i8.u32[1] = i8.u32[1] +
446                         (uint8_t)tbl[1] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
447                 tbl[1] = *(const uint16_t *)&lpm->tbl8[i8.u32[1]];
448         }
449         if (unlikely((pt >> 32 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
450                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
451                 i8.u32[2] = i8.u32[2] +
452                         (uint8_t)tbl[2] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
453                 tbl[2] = *(const uint16_t *)&lpm->tbl8[i8.u32[2]];
454         }
455         if (unlikely((pt >> 48 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
456                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
457                 i8.u32[3] = i8.u32[3] +
458                         (uint8_t)tbl[3] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
459                 tbl[3] = *(const uint16_t *)&lpm->tbl8[i8.u32[3]];
460         }
461
462         hop[0] = (tbl[0] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[0] : defv;
463         hop[1] = (tbl[1] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[1] : defv;
464         hop[2] = (tbl[2] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[2] : defv;
465         hop[3] = (tbl[3] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[3] : defv;
466 }
467
468 #ifdef __cplusplus
469 }
470 #endif
471
472 #endif /* _RTE_LPM_H_ */