DPDK  17.11.10
rte_lpm.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_LPM_H_
35 #define _RTE_LPM_H_
36 
42 #include <errno.h>
43 #include <sys/queue.h>
44 #include <stdint.h>
45 #include <stdlib.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_byteorder.h>
48 #include <rte_config.h>
49 #include <rte_memory.h>
50 #include <rte_common.h>
51 #include <rte_vect.h>
52 #include <rte_compat.h>
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
59 #define RTE_LPM_NAMESIZE 32
60 
62 #define RTE_LPM_MAX_DEPTH 32
63 
65 #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
66 
68 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
69 
71 #define RTE_LPM_MAX_TBL8_NUM_GROUPS (1 << 24)
72 
74 #define RTE_LPM_TBL8_NUM_GROUPS 256
75 
77 #define RTE_LPM_TBL8_NUM_ENTRIES (RTE_LPM_TBL8_NUM_GROUPS * \
78  RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
79 
81 #if defined(RTE_LIBRTE_LPM_DEBUG)
82 #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
83  if (cond) return (retval); \
84 } while (0)
85 #else
86 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
87 #endif
88 
90 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x03000000
91 
93 #define RTE_LPM_LOOKUP_SUCCESS 0x01000000
94 
95 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
96 
97 __extension__
98 struct rte_lpm_tbl_entry_v20 {
105  union {
106  uint8_t next_hop;
107  uint8_t group_idx;
108  };
109  /* Using single uint8_t to store 3 values. */
110  uint8_t valid :1;
118  uint8_t valid_group :1;
119  uint8_t depth :6;
120 };
121 
122 __extension__
123 struct rte_lpm_tbl_entry {
129  uint32_t next_hop :24;
130  /* Using single uint8_t to store 3 values. */
131  uint32_t valid :1;
139  uint32_t valid_group :1;
140  uint32_t depth :6;
141 };
142 
143 #else
144 __extension__
145 struct rte_lpm_tbl_entry_v20 {
146  uint8_t depth :6;
147  uint8_t valid_group :1;
148  uint8_t valid :1;
149  union {
150  uint8_t group_idx;
151  uint8_t next_hop;
152  };
153 };
154 
155 __extension__
156 struct rte_lpm_tbl_entry {
157  uint32_t depth :6;
158  uint32_t valid_group :1;
159  uint32_t valid :1;
160  uint32_t next_hop :24;
161 
162 };
163 
164 #endif
165 
168  uint32_t max_rules;
169  uint32_t number_tbl8s;
170  int flags;
171 };
172 
174 struct rte_lpm_rule_v20 {
175  uint32_t ip;
176  uint8_t next_hop;
177 };
178 
179 struct rte_lpm_rule {
180  uint32_t ip;
181  uint32_t next_hop;
182 };
183 
185 struct rte_lpm_rule_info {
186  uint32_t used_rules;
187  uint32_t first_rule;
188 };
189 
191 struct rte_lpm_v20 {
192  /* LPM metadata. */
193  char name[RTE_LPM_NAMESIZE];
194  uint32_t max_rules;
195  struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH];
197  /* LPM Tables. */
198  struct rte_lpm_tbl_entry_v20 tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
200  struct rte_lpm_tbl_entry_v20 tbl8[RTE_LPM_TBL8_NUM_ENTRIES]
202  struct rte_lpm_rule_v20 rules_tbl[]
204 };
205 
206 struct rte_lpm {
207  /* LPM metadata. */
208  char name[RTE_LPM_NAMESIZE];
209  uint32_t max_rules;
210  uint32_t number_tbl8s;
211  struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH];
213  /* LPM Tables. */
214  struct rte_lpm_tbl_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES]
216  struct rte_lpm_tbl_entry *tbl8;
217  struct rte_lpm_rule *rules_tbl;
218 };
219 
239 struct rte_lpm *
240 rte_lpm_create(const char *name, int socket_id,
241  const struct rte_lpm_config *config);
242 struct rte_lpm_v20 *
243 rte_lpm_create_v20(const char *name, int socket_id, int max_rules, int flags);
244 struct rte_lpm *
245 rte_lpm_create_v1604(const char *name, int socket_id,
246  const struct rte_lpm_config *config);
247 
258 struct rte_lpm *
259 rte_lpm_find_existing(const char *name);
260 struct rte_lpm_v20 *
261 rte_lpm_find_existing_v20(const char *name);
262 struct rte_lpm *
263 rte_lpm_find_existing_v1604(const char *name);
264 
273 void
274 rte_lpm_free(struct rte_lpm *lpm);
275 void
276 rte_lpm_free_v20(struct rte_lpm_v20 *lpm);
277 void
278 rte_lpm_free_v1604(struct rte_lpm *lpm);
279 
294 int
295 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint32_t next_hop);
296 int
297 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
298  uint8_t next_hop);
299 int
300 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
301  uint32_t next_hop);
302 
318 int
319 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
320 uint32_t *next_hop);
321 int
322 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
323 uint8_t *next_hop);
324 int
325 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
326 uint32_t *next_hop);
327 
340 int
341 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
342 int
343 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth);
344 int
345 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
346 
353 void
354 rte_lpm_delete_all(struct rte_lpm *lpm);
355 void
356 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm);
357 void
358 rte_lpm_delete_all_v1604(struct rte_lpm *lpm);
359 
372 static inline int
373 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
374 {
375  unsigned tbl24_index = (ip >> 8);
376  uint32_t tbl_entry;
377  const uint32_t *ptbl;
378 
379  /* DEBUG: Check user input arguments. */
380  RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (next_hop == NULL)), -EINVAL);
381 
382  /* Copy tbl24 entry */
383  ptbl = (const uint32_t *)(&lpm->tbl24[tbl24_index]);
384  tbl_entry = *ptbl;
385 
386  /* Copy tbl8 entry (only if needed) */
387  if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
388  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
389 
390  unsigned tbl8_index = (uint8_t)ip +
391  (((uint32_t)tbl_entry & 0x00FFFFFF) *
392  RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
393 
394  ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
395  tbl_entry = *ptbl;
396  }
397 
398  *next_hop = ((uint32_t)tbl_entry & 0x00FFFFFF);
399  return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
400 }
401 
422 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
423  rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
424 
425 static inline int
426 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t *ips,
427  uint32_t *next_hops, const unsigned n)
428 {
429  unsigned i;
430  unsigned tbl24_indexes[n];
431  const uint32_t *ptbl;
432 
433  /* DEBUG: Check user input arguments. */
434  RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
435  (next_hops == NULL)), -EINVAL);
436 
437  for (i = 0; i < n; i++) {
438  tbl24_indexes[i] = ips[i] >> 8;
439  }
440 
441  for (i = 0; i < n; i++) {
442  /* Simply copy tbl24 entry to output */
443  ptbl = (const uint32_t *)&lpm->tbl24[tbl24_indexes[i]];
444  next_hops[i] = *ptbl;
445 
446  /* Overwrite output with tbl8 entry if needed */
447  if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
448  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
449 
450  unsigned tbl8_index = (uint8_t)ips[i] +
451  (((uint32_t)next_hops[i] & 0x00FFFFFF) *
452  RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
453 
454  ptbl = (const uint32_t *)&lpm->tbl8[tbl8_index];
455  next_hops[i] = *ptbl;
456  }
457  }
458  return 0;
459 }
460 
461 /* Mask four results. */
462 #define RTE_LPM_MASKX4_RES UINT64_C(0x00ffffff00ffffff)
463 
483 static inline void
484 rte_lpm_lookupx4(const struct rte_lpm *lpm, xmm_t ip, uint32_t hop[4],
485  uint32_t defv);
486 
487 #if defined(RTE_ARCH_ARM) || defined(RTE_ARCH_ARM64)
488 #include "rte_lpm_neon.h"
489 #elif defined(RTE_ARCH_PPC_64)
490 #include "rte_lpm_altivec.h"
491 #else
492 #include "rte_lpm_sse.h"
493 #endif
494 
495 #ifdef __cplusplus
496 }
497 #endif
498 
499 #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)
struct rte_mbuf __rte_cache_aligned
uint32_t max_rules
Definition: rte_lpm.h:168
#define RTE_LPM_MAX_DEPTH
Definition: rte_lpm.h:62
static int rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint32_t *next_hop)
Definition: rte_lpm.h:373
#define unlikely(x)
#define RTE_LPM_NAMESIZE
Definition: rte_lpm.h:59
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:169
int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
#define RTE_STD_C11
Definition: rte_common.h:66
#define RTE_LPM_LOOKUP_SUCCESS
Definition: rte_lpm.h:93
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)