DPDK 25.03.0-rc1
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
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
25extern "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__
79struct rte_lpm_tbl_entry {
85 uint32_t next_hop :24;
86 /* Using single uint8_t to store 3 values. */
87 uint32_t valid :1;
95 uint32_t valid_group :1;
96 uint32_t depth :6;
97};
98
99#else
100
101__extension__
102struct rte_lpm_tbl_entry {
103 uint32_t depth :6;
104 uint32_t valid_group :1;
105 uint32_t valid :1;
106 uint32_t next_hop :24;
107
108};
109
110#endif
111
114 uint32_t max_rules;
115 uint32_t number_tbl8s;
116 int flags;
117};
118
120struct rte_lpm {
121 /* LPM Tables. */
122 alignas(RTE_CACHE_LINE_SIZE) struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES];
124 struct rte_lpm_tbl_entry *tbl8;
125};
126
129 struct rte_rcu_qsbr *v; /* RCU QSBR variable. */
130 /* Mode of RCU QSBR. RTE_LPM_QSBR_MODE_xxx
131 * '0' for default: create defer queue for reclaim.
132 */
133 enum rte_lpm_qsbr_mode mode;
134 uint32_t dq_size; /* RCU defer queue size.
135 * default: lpm->number_tbl8s.
136 */
137 uint32_t reclaim_thd; /* Threshold to trigger auto reclaim. */
138 uint32_t reclaim_max; /* Max entries to reclaim in one go.
139 * default: RTE_LPM_RCU_DQ_RECLAIM_MAX.
140 */
141};
142
150void
151rte_lpm_free(struct rte_lpm *lpm);
152
172struct rte_lpm *
173rte_lpm_create(const char *name, int socket_id,
174 const struct rte_lpm_config *config)
176
187struct rte_lpm *
188rte_lpm_find_existing(const char *name);
189
205int rte_lpm_rcu_qsbr_add(struct rte_lpm *lpm, struct rte_lpm_rcu_config *cfg);
206
221int
222rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
223
239int
240rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
241uint32_t *next_hop);
242
255int
256rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
257
264void
265rte_lpm_delete_all(struct rte_lpm *lpm);
266
279static inline int
280rte_lpm_lookup(const struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
281{
282 unsigned tbl24_index = (ip >> 8);
283 uint32_t tbl_entry;
284 const uint32_t *ptbl;
285
286 /* DEBUG: Check user input arguments. */
287 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
288
289 /* Copy tbl24 entry */
290 ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
291 tbl_entry = *ptbl;
292
293 /* Memory ordering is not required in lookup. Because dataflow
294 * dependency exists, compiler or HW won't be able to re-order
295 * the operations.
296 */
297 /* Copy tbl8 entry (only if needed) */
298 if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
299 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
300
301 unsigned tbl8_index = (uint8_t)ip +
302 (((uint32_t)tbl_entry & 0x00FFFFFF) *
303 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
304
305 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
306 tbl_entry = *ptbl;
307 }
308
309 *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
310 return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
311}
312
333#define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
334 rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
335
336static inline int
337rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
338 uint32_t *next_hops, const unsigned n)
339{
340 unsigned i;
341 const uint32_t *ptbl;
342
343 /* DEBUG: Check user input arguments. */
344 RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
345 (next_hops == NULL)), -EINVAL);
346
347 for (i = 0; i < n; i++) {
348 unsigned int tbl24_index = ips[i] >> 8;
349
350 /* Simply copy tbl24 entry to output */
351 ptbl = (const uint32_t *)&lpm->tbl24[tbl24_index];
352 next_hops[i] = *ptbl;
353
354 /* Overwrite output with tbl8 entry if needed */
355 if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
356 RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
357
358 unsigned tbl8_index = (uint8_t)ips[i] +
359 (((uint32_t)next_hops[i] & 0x00FFFFFF) *
360 RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
361
362 ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
363 next_hops[i] = *ptbl;
364 }
365 }
366 return 0;
367}
368
369/* Mask four results. */
370#define RTE_LPM_MASKX4_RES UINT64_C(0x00ffffff00ffffff)
371
391static inline void
392rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
393 uint32_t defv);
394
395#ifdef __cplusplus
396}
397#endif
398
399#if defined(RTE_ARCH_ARM)
400#ifdef RTE_HAS_SVE_ACLE
401#include "rte_lpm_sve.h"
402#undef rte_lpm_lookup_bulk
403#define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
404 __rte_lpm_lookup_vec(lpm, ips, next_hops, n)
405#endif
406#include "rte_lpm_neon.h"
407#elif defined(RTE_ARCH_PPC_64)
408#include "rte_lpm_altivec.h"
409#elif defined(RTE_ARCH_X86)
410#include "rte_lpm_sse.h"
411#else
412#include "rte_lpm_scalar.h"
413#endif
414
415#endif /* _RTE_LPM_H_ */
#define unlikely(x)
#define __rte_dealloc(dealloc, argno)
Definition: rte_common.h:301
#define __rte_malloc
Definition: rte_common.h:290
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)
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)
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
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:280
struct rte_lpm struct rte_lpm * rte_lpm_find_existing(const char *name)
uint32_t number_tbl8s
Definition: rte_lpm.h:115
uint32_t max_rules
Definition: rte_lpm.h:114