DPDK  19.11.14
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  */
4 
5 #ifndef _RTE_LPM_H_
6 #define _RTE_LPM_H_
7 
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 
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 
65 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
66 
67 __extension__
68 struct rte_lpm_tbl_entry {
74  uint32_t next_hop :24;
75  /* Using single uint8_t to store 3 values. */
76  uint32_t valid :1;
84  uint32_t valid_group :1;
85  uint32_t depth :6;
86 };
87 
88 #else
89 
90 __extension__
91 struct rte_lpm_tbl_entry {
92  uint32_t depth :6;
93  uint32_t valid_group :1;
94  uint32_t valid :1;
95  uint32_t next_hop :24;
96 
97 };
98 
99 #endif
100 
103  uint32_t max_rules;
104  uint32_t number_tbl8s;
105  int flags;
106 };
107 
109 struct rte_lpm_rule {
110  uint32_t ip;
111  uint32_t next_hop;
112 };
113 
115 struct rte_lpm_rule_info {
116  uint32_t used_rules;
117  uint32_t first_rule;
118 };
119 
121 struct rte_lpm {
122  /* LPM metadata. */
123  char name[RTE_LPM_NAMESIZE];
124  uint32_t max_rules;
125  uint32_t number_tbl8s;
126  struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH];
128  /* LPM Tables. */
129  struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
130  __rte_cache_aligned;
131  struct rte_lpm_tbl_entry *tbl8;
132  struct rte_lpm_rule *rules_tbl;
133 };
134 
154 struct rte_lpm *
155 rte_lpm_create(const char *name, int socket_id,
156  const struct rte_lpm_config *config);
157 
168 struct rte_lpm *
169 rte_lpm_find_existing(const char *name);
170 
179 void
180 rte_lpm_free(struct rte_lpm *lpm);
181 
196 int
197 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
198 
214 int
215 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
216 uint32_t *next_hop);
217 
230 int
231 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
232 
239 void
240 rte_lpm_delete_all(struct rte_lpm *lpm);
241 
254 static inline int
255 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
256 {
257  unsigned tbl24_index = (ip >> 8);
258  uint32_t tbl_entry;
259  const uint32_t *ptbl;
260 
261  /* DEBUG: Check user input arguments. */
262  RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
263 
264  /* Copy tbl24 entry */
265  ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
266  tbl_entry = *ptbl;
267 
268  /* Memory ordering is not required in lookup. Because dataflow
269  * dependency exists, compiler or HW won't be able to re-order
270  * the operations.
271  */
272  /* Copy tbl8 entry (only if needed) */
273  if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
274  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
275 
276  unsigned tbl8_index = (uint8_t)ip +
277  (((uint32_t)tbl_entry & 0x00FFFFFF) *
278  RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
279 
280  ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
281  tbl_entry = *ptbl;
282  }
283 
284  *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
285  return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
286 }
287 
308 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
309  rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
310 
311 static inline int
312 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
313  uint32_t *next_hops, const unsigned n)
314 {
315  unsigned i;
316  unsigned tbl24_indexes[n];
317  const uint32_t *ptbl;
318 
319  /* DEBUG: Check user input arguments. */
320  RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
321  (next_hops == NULL)), -EINVAL);
322 
323  for (i = 0; i < n; i++) {
324  tbl24_indexes[i] = ips[i] >> 8;
325  }
326 
327  for (i = 0; i < n; i++) {
328  /* Simply copy tbl24 entry to output */
329  ptbl = (const uint32_t *)&lpm->tbl24[tbl24_indexes[i]];
330  next_hops[i] = *ptbl;
331 
332  /* Overwrite output with tbl8 entry if needed */
333  if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
334  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
335 
336  unsigned tbl8_index = (uint8_t)ips[i] +
337  (((uint32_t)next_hops[i] & 0x00FFFFFF) *
338  RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
339 
340  ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
341  next_hops[i] = *ptbl;
342  }
343  }
344  return 0;
345 }
346 
347 /* Mask four results. */
348 #define RTE_LPM_MASKX4_RES UINT64_C(0x00ffffff00ffffff)
349 
369 static inline void
370 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
371  uint32_t defv);
372 
373 #if defined(RTE_ARCH_ARM) || defined(RTE_ARCH_ARM64)
374 #include "rte_lpm_neon.h"
375 #elif defined(RTE_ARCH_PPC_64)
376 #include "rte_lpm_altivec.h"
377 #else
378 #include "rte_lpm_sse.h"
379 #endif
380 
381 #ifdef __cplusplus
382 }
383 #endif
384 
385 #endif /* _RTE_LPM_H_ */
void rte_lpm_free(struct rte_lpm *lpm)
struct rte_lpm * rte_lpm_create(const char *name, int socket_id, const struct rte_lpm_config *config)
void rte_lpm_delete_all(struct rte_lpm *lpm)
uint32_t max_rules
Definition: rte_lpm.h:103
static int rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
Definition: rte_lpm.h:255
#define unlikely(x)
#define RTE_LPM_NAMESIZE
Definition: rte_lpm.h:29
static void rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4], uint32_t defv)
uint32_t number_tbl8s
Definition: rte_lpm.h:104
int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
#define RTE_LPM_LOOKUP_SUCCESS
Definition: rte_lpm.h:63
int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop)
int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t *next_hop)
struct rte_lpm * rte_lpm_find_existing(const char *name)