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