DPDK  25.11.0
rte_lpm.h
Go to the documentation of this file.
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 
14 #include <errno.h>
15 #include <stdalign.h>
16 #include <stdint.h>
17 
18 #include <rte_branch_prediction.h>
19 #include <rte_byteorder.h>
20 #include <rte_common.h>
21 #include <rte_vect.h>
22 #include <rte_rcu_qsbr.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
29 #define RTE_LPM_NAMESIZE 32
30 
32 #define RTE_LPM_MAX_DEPTH 32
33 
35 #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
36 
38 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
39 
41 #define RTE_LPM_MAX_TBL8_NUM_GROUPS (1 << 24)
42 
44 #define RTE_LPM_TBL8_NUM_GROUPS 256
45 
47 #define RTE_LPM_TBL8_NUM_ENTRIES (RTE_LPM_TBL8_NUM_GROUPS * \
48  RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
49 
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 
60 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x03000000
61 
63 #define RTE_LPM_LOOKUP_SUCCESS 0x01000000
64 
66 #define RTE_LPM_RCU_DQ_RECLAIM_MAX 16
67 
74 };
75 
76 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
78 __extension__
79 struct rte_lpm_tbl_entry {
80  union {
81  RTE_ATOMIC(uint32_t) val;
82  struct {
88  uint32_t next_hop :24;
89  /* Using single uint8_t to store 3 values. */
90  uint32_t valid :1;
98  uint32_t valid_group :1;
99  uint32_t depth :6;
100  };
101  };
102 };
103 
104 #else
105 
106 __extension__
107 struct rte_lpm_tbl_entry {
108  union {
109  RTE_ATOMIC(uint32_t) val;
110  struct {
111  uint32_t depth :6;
112  uint32_t valid_group :1;
113  uint32_t valid :1;
114  uint32_t next_hop :24;
115  };
116  };
117 };
118 
119 #endif
120 
121 static_assert(sizeof(struct rte_lpm_tbl_entry) == sizeof(uint32_t),
122  "sizeof(struct rte_lpm_tbl_entry) == sizeof(uint32_t)");
123 
126  uint32_t max_rules;
127  uint32_t number_tbl8s;
128  int flags;
129 };
130 
132 struct rte_lpm {
133  /* LPM Tables. */
134  alignas(RTE_CACHE_LINE_SIZE) struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES];
136  struct rte_lpm_tbl_entry *tbl8;
137 };
138 
141  struct rte_rcu_qsbr *v; /* RCU QSBR variable. */
142  /* Mode of RCU QSBR. RTE_LPM_QSBR_MODE_xxx
143  * '0' for default: create defer queue for reclaim.
144  */
145  enum rte_lpm_qsbr_mode mode;
146  uint32_t dq_size; /* RCU defer queue size.
147  * default: lpm->number_tbl8s.
148  */
149  uint32_t reclaim_thd; /* Threshold to trigger auto reclaim. */
150  uint32_t reclaim_max; /* Max entries to reclaim in one go.
151  * default: RTE_LPM_RCU_DQ_RECLAIM_MAX.
152  */
153 };
154 
162 void
163 rte_lpm_free(struct rte_lpm *lpm);
164 
184 struct rte_lpm *
185 rte_lpm_create(const char *name, int socket_id,
186  const struct rte_lpm_config *config)
188 
199 struct rte_lpm *
200 rte_lpm_find_existing(const char *name);
201 
217 int rte_lpm_rcu_qsbr_add(struct rte_lpm *lpm, struct rte_lpm_rcu_config *cfg);
218 
233 int
234 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
235 
251 int
252 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
253 uint32_t *next_hop);
254 
267 int
268 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
269 
276 void
277 rte_lpm_delete_all(struct rte_lpm *lpm);
278 
291 static inline int
292 rte_lpm_lookup(const struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
293 {
294  unsigned tbl24_index = (ip >> 8);
295  uint32_t tbl_entry;
296  const uint32_t *ptbl;
297 
298  /* DEBUG: Check user input arguments. */
299  RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
300 
301  /* Copy tbl24 entry */
302  ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
303  tbl_entry = *ptbl;
304 
305  /* Memory ordering is not required in lookup. Because dataflow
306  * dependency exists, compiler or HW won't be able to re-order
307  * the operations.
308  */
309  /* Copy tbl8 entry (only if needed) */
310  if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
311  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
312 
313  unsigned tbl8_index = (uint8_t)ip +
314  (((uint32_t)tbl_entry & 0x00FFFFFF) *
315  RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
316 
317  ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
318  tbl_entry = *ptbl;
319  }
320 
321  *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
322  return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
323 }
324 
345 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
346  rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
347 
348 static inline int
349 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
350  uint32_t *next_hops, const unsigned n)
351 {
352  unsigned i;
353  const uint32_t *ptbl;
354 
355  /* DEBUG: Check user input arguments. */
356  RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
357  (next_hops == NULL)), -EINVAL);
358 
359  for (i = 0; i < n; i++) {
360  unsigned int tbl24_index = ips[i] >> 8;
361 
362  /* Simply copy tbl24 entry to output */
363  ptbl = (const uint32_t *)&lpm->tbl24[tbl24_index];
364  next_hops[i] = *ptbl;
365 
366  /* Overwrite output with tbl8 entry if needed */
367  if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
368  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
369 
370  unsigned tbl8_index = (uint8_t)ips[i] +
371  (((uint32_t)next_hops[i] & 0x00FFFFFF) *
372  RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
373 
374  ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
375  next_hops[i] = *ptbl;
376  }
377  }
378  return 0;
379 }
380 
381 /* Mask four results. */
382 #define RTE_LPM_MASKX4_RES UINT64_C(0x00ffffff00ffffff)
383 
403 static inline void
404 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
405  uint32_t defv);
406 
407 #ifdef __cplusplus
408 }
409 #endif
410 
411 #if defined(RTE_ARCH_ARM)
412 #ifdef RTE_HAS_SVE_ACLE
413 #include "rte_lpm_sve.h"
414 #undef rte_lpm_lookup_bulk
415 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
416  __rte_lpm_lookup_vec(lpm, ips, next_hops, n)
417 #endif
418 #include "rte_lpm_neon.h"
419 #elif defined(RTE_ARCH_PPC_64)
420 #include "rte_lpm_altivec.h"
421 #elif defined(RTE_ARCH_X86)
422 #include "rte_lpm_sse.h"
423 #elif defined(RTE_ARCH_RISCV) && defined(RTE_RISCV_FEATURE_V)
424 #include "rte_lpm_rvv.h"
425 #else
426 #include "rte_lpm_scalar.h"
427 #endif
428 
429 #endif /* _RTE_LPM_H_ */
#define unlikely(x)
#define __rte_dealloc(dealloc, argno)
Definition: rte_common.h:339
#define __rte_malloc
Definition: rte_common.h:328
int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop)
int rte_lpm_rcu_qsbr_add(struct rte_lpm *lpm, struct rte_lpm_rcu_config *cfg)
int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
struct rte_lpm * rte_lpm_create(const char *name, int socket_id, const struct rte_lpm_config *config) __rte_malloc __rte_dealloc(rte_lpm_free
struct rte_lpm struct rte_lpm * rte_lpm_find_existing(const char *name)
void rte_lpm_free(struct rte_lpm *lpm)
static void rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4], uint32_t defv)
#define RTE_LPM_LOOKUP_SUCCESS
Definition: rte_lpm.h:63
rte_lpm_qsbr_mode
Definition: rte_lpm.h:69
@ RTE_LPM_QSBR_MODE_DQ
Definition: rte_lpm.h:71
@ RTE_LPM_QSBR_MODE_SYNC
Definition: rte_lpm.h:73
void rte_lpm_delete_all(struct rte_lpm *lpm)
int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t *next_hop)
static int rte_lpm_lookup(const struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
Definition: rte_lpm.h:292
uint32_t number_tbl8s
Definition: rte_lpm.h:127
uint32_t max_rules
Definition: rte_lpm.h:126