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