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