lpm: introduce rte_lpm_lookupx4
[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         TAILQ_ENTRY(rte_lpm) next;      /**< Next in list. */
136
137         /* LPM metadata. */
138         char name[RTE_LPM_NAMESIZE];        /**< Name of the lpm. */
139         int mem_location; /**< @deprecated @see RTE_LPM_HEAP and RTE_LPM_MEMZONE. */
140         uint32_t max_rules; /**< Max. balanced rules per lpm. */
141         struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH]; /**< Rule info table. */
142
143         /* LPM Tables. */
144         struct rte_lpm_tbl24_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES] \
145                         __rte_cache_aligned; /**< LPM tbl24 table. */
146         struct rte_lpm_tbl8_entry tbl8[RTE_LPM_TBL8_NUM_ENTRIES] \
147                         __rte_cache_aligned; /**< LPM tbl8 table. */
148         struct rte_lpm_rule rules_tbl[0] \
149                         __rte_cache_aligned; /**< LPM rules. */
150 };
151
152 /**
153  * Create an LPM object.
154  *
155  * @param name
156  *   LPM object name
157  * @param socket_id
158  *   NUMA socket ID for LPM table memory allocation
159  * @param max_rules
160  *   Maximum number of LPM rules that can be added
161  * @param flags
162  *   This parameter is currently unused
163  * @return
164  *   Handle to LPM object on success, NULL otherwise with rte_errno set
165  *   to an appropriate values. Possible rte_errno values include:
166  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
167  *    - E_RTE_SECONDARY - function was called from a secondary process instance
168  *    - E_RTE_NO_TAILQ - no tailq list could be got for the lpm object list
169  *    - EINVAL - invalid parameter passed to function
170  *    - ENOSPC - the maximum number of memzones has already been allocated
171  *    - EEXIST - a memzone with the same name already exists
172  *    - ENOMEM - no appropriate memory area found in which to create memzone
173  */
174 struct rte_lpm *
175 rte_lpm_create(const char *name, int socket_id, int max_rules, int flags);
176
177 /**
178  * Find an existing LPM object and return a pointer to it.
179  *
180  * @param name
181  *   Name of the lpm object as passed to rte_lpm_create()
182  * @return
183  *   Pointer to lpm object or NULL if object not found with rte_errno
184  *   set appropriately. Possible rte_errno values include:
185  *    - ENOENT - required entry not available to return.
186  */
187 struct rte_lpm *
188 rte_lpm_find_existing(const char *name);
189
190 /**
191  * Free an LPM object.
192  *
193  * @param lpm
194  *   LPM object handle
195  * @return
196  *   None
197  */
198 void
199 rte_lpm_free(struct rte_lpm *lpm);
200
201 /**
202  * Add a rule to the LPM table.
203  *
204  * @param lpm
205  *   LPM object handle
206  * @param ip
207  *   IP of the rule to be added to the LPM table
208  * @param depth
209  *   Depth of the rule to be added to the LPM table
210  * @param next_hop
211  *   Next hop of the rule to be added to the LPM table
212  * @return
213  *   0 on success, negative value otherwise
214  */
215 int
216 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t next_hop);
217
218 /**
219  * Delete a rule from the LPM table.
220  *
221  * @param lpm
222  *   LPM object handle
223  * @param ip
224  *   IP of the rule to be deleted from the LPM table
225  * @param depth
226  *   Depth of the rule to be deleted from the LPM table
227  * @return
228  *   0 on success, negative value otherwise
229  */
230 int
231 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
232
233 /**
234  * Delete all rules from the LPM table.
235  *
236  * @param lpm
237  *   LPM object handle
238  */
239 void
240 rte_lpm_delete_all(struct rte_lpm *lpm);
241
242 /**
243  * Lookup an IP into the LPM table.
244  *
245  * @param lpm
246  *   LPM object handle
247  * @param ip
248  *   IP to be looked up in the LPM table
249  * @param next_hop
250  *   Next hop of the most specific rule found for IP (valid on lookup hit only)
251  * @return
252  *   -EINVAL for incorrect arguments, -ENOENT on lookup miss, 0 on lookup hit
253  */
254 static inline int
255 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
256 {
257         unsigned tbl24_index = (ip >> 8);
258         uint16_t tbl_entry;
259
260         /* DEBUG: Check user input arguments. */
261         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
262
263         /* Copy tbl24 entry */
264         tbl_entry = *(const uint16_t *)&lpm->tbl24[tbl24_index];
265
266         /* Copy tbl8 entry (only if needed) */
267         if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
268                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
269
270                 unsigned tbl8_index = (uint8_t)ip +
271                                 ((uint8_t)tbl_entry * RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
272
273                 tbl_entry = *(const uint16_t *)&lpm->tbl8[tbl8_index];
274         }
275
276         *next_hop = (uint8_t)tbl_entry;
277         return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
278 }
279
280 /**
281  * Lookup multiple IP addresses in an LPM table. This may be implemented as a
282  * macro, so the address of the function should not be used.
283  *
284  * @param lpm
285  *   LPM object handle
286  * @param ips
287  *   Array of IPs to be looked up in the LPM table
288  * @param next_hops
289  *   Next hop of the most specific rule found for IP (valid on lookup hit only).
290  *   This is an array of two byte values. The most significant byte in each
291  *   value says whether the lookup was successful (bitmask
292  *   RTE_LPM_LOOKUP_SUCCESS is set). The least significant byte is the
293  *   actual next hop.
294  * @param n
295  *   Number of elements in ips (and next_hops) array to lookup. This should be a
296  *   compile time constant, and divisible by 8 for best performance.
297  *  @return
298  *   -EINVAL for incorrect arguments, otherwise 0
299  */
300 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
301                 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
302
303 static inline int
304 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t * ips,
305                 uint16_t * next_hops, const unsigned n)
306 {
307         unsigned i;
308         unsigned tbl24_indexes[n];
309
310         /* DEBUG: Check user input arguments. */
311         RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
312                         (next_hops == NULL)), -EINVAL);
313
314         for (i = 0; i < n; i++) {
315                 tbl24_indexes[i] = ips[i] >> 8;
316         }
317
318         for (i = 0; i < n; i++) {
319                 /* Simply copy tbl24 entry to output */
320                 next_hops[i] = *(const uint16_t *)&lpm->tbl24[tbl24_indexes[i]];
321
322                 /* Overwrite output with tbl8 entry if needed */
323                 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
324                                 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
325
326                         unsigned tbl8_index = (uint8_t)ips[i] +
327                                         ((uint8_t)next_hops[i] *
328                                          RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
329
330                         next_hops[i] = *(const uint16_t *)&lpm->tbl8[tbl8_index];
331                 }
332         }
333         return 0;
334 }
335
336 /* Mask four results. */
337 #define  RTE_LPM_MASKX4_RES     UINT64_C(0x00ff00ff00ff00ff)
338
339 /**
340  * Lookup four IP addresses in an LPM table.
341  *
342  * @param lpm
343  *   LPM object handle
344  * @param ip
345  *   Four IPs to be looked up in the LPM table
346  * @param hop
347  *   Next hop of the most specific rule found for IP (valid on lookup hit only).
348  *   This is an 4 elements array of two byte values.
349  *   If the lookup was succesfull for the given IP, then least significant byte
350  *   of the corresponding element is the  actual next hop and the most
351  *   significant byte is zero.
352  *   If the lookup for the given IP failed, then corresponding element would
353  *   contain default value, see description of then next parameter.
354  * @param defv
355  *   Default value to populate into corresponding element of hop[] array,
356  *   if lookup would fail.
357  */
358 static inline void
359 rte_lpm_lookupx4(const struct rte_lpm *lpm, __m128i ip, uint16_t hop[4],
360         uint16_t defv)
361 {
362         __m128i i24;
363         rte_xmm_t i8;
364         uint16_t tbl[4];
365         uint64_t idx, pt;
366
367         const __m128i mask8 =
368                 _mm_set_epi32(UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX);
369
370         /*
371          * RTE_LPM_VALID_EXT_ENTRY_BITMASK for 4 LPM entries
372          * as one 64-bit value (0x0300030003000300).
373          */
374         const uint64_t mask_xv =
375                 ((uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK |
376                 (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 16 |
377                 (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 32 |
378                 (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 48);
379
380         /*
381          * RTE_LPM_LOOKUP_SUCCESS for 4 LPM entries
382          * as one 64-bit value (0x0100010001000100).
383          */
384         const uint64_t mask_v =
385                 ((uint64_t)RTE_LPM_LOOKUP_SUCCESS |
386                 (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 16 |
387                 (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 32 |
388                 (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 48);
389
390         /* get 4 indexes for tbl24[]. */
391         i24 = _mm_srli_epi32(ip, CHAR_BIT);
392
393         /* extract values from tbl24[] */
394         idx = _mm_cvtsi128_si64(i24);
395         i24 = _mm_srli_si128(i24, sizeof(uint64_t));
396
397         tbl[0] = *(const uint16_t *)&lpm->tbl24[(uint32_t)idx];
398         tbl[1] = *(const uint16_t *)&lpm->tbl24[idx >> 32];
399
400         idx = _mm_cvtsi128_si64(i24);
401
402         tbl[2] = *(const uint16_t *)&lpm->tbl24[(uint32_t)idx];
403         tbl[3] = *(const uint16_t *)&lpm->tbl24[idx >> 32];
404
405         /* get 4 indexes for tbl8[]. */
406         i8.m = _mm_and_si128(ip, mask8);
407
408         pt = (uint64_t)tbl[0] |
409                 (uint64_t)tbl[1] << 16 |
410                 (uint64_t)tbl[2] << 32 |
411                 (uint64_t)tbl[3] << 48;
412
413         /* search successfully finished for all 4 IP addresses. */
414         if (likely((pt & mask_xv) == mask_v)) {
415                 uintptr_t ph = (uintptr_t)hop;
416                 *(uint64_t *)ph = pt & RTE_LPM_MASKX4_RES;
417                 return;
418         }
419
420         if (unlikely((pt & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
421                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
422                 i8.u32[0] = i8.u32[0] +
423                         (uint8_t)tbl[0] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
424                 tbl[0] = *(const uint16_t *)&lpm->tbl8[i8.u32[0]];
425         }
426         if (unlikely((pt >> 16 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
427                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
428                 i8.u32[1] = i8.u32[1] +
429                         (uint8_t)tbl[1] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
430                 tbl[1] = *(const uint16_t *)&lpm->tbl8[i8.u32[1]];
431         }
432         if (unlikely((pt >> 32 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
433                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
434                 i8.u32[2] = i8.u32[2] +
435                         (uint8_t)tbl[2] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
436                 tbl[2] = *(const uint16_t *)&lpm->tbl8[i8.u32[2]];
437         }
438         if (unlikely((pt >> 48 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
439                         RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
440                 i8.u32[3] = i8.u32[3] +
441                         (uint8_t)tbl[3] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
442                 tbl[3] = *(const uint16_t *)&lpm->tbl8[i8.u32[3]];
443         }
444
445         hop[0] = (tbl[0] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[0] : defv;
446         hop[1] = (tbl[1] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[1] : defv;
447         hop[2] = (tbl[2] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[2] : defv;
448         hop[3] = (tbl[3] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[3] : defv;
449 }
450
451 #ifdef __cplusplus
452 }
453 #endif
454
455 #endif /* _RTE_LPM_H_ */