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