DPDK  make-f/builddir/build/BUILD/dpdk-2.2.0/mk/rte.sdkconfig.mkshowversion
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 <string.h>
47 #include <rte_branch_prediction.h>
48 #include <rte_byteorder.h>
49 #include <rte_memory.h>
50 #include <rte_common.h>
51 #include <rte_vect.h>
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
58 #define RTE_LPM_NAMESIZE 32
59 
61 #define RTE_LPM_MAX_DEPTH 32
62 
64 #define RTE_LPM_TBL24_NUM_ENTRIES (1 << 24)
65 
67 #define RTE_LPM_TBL8_GROUP_NUM_ENTRIES 256
68 
70 #define RTE_LPM_TBL8_NUM_GROUPS 256
71 
73 #define RTE_LPM_TBL8_NUM_ENTRIES (RTE_LPM_TBL8_NUM_GROUPS * \
74  RTE_LPM_TBL8_GROUP_NUM_ENTRIES)
75 
77 #if defined(RTE_LIBRTE_LPM_DEBUG)
78 #define RTE_LPM_RETURN_IF_TRUE(cond, retval) do { \
79  if (cond) return (retval); \
80 } while (0)
81 #else
82 #define RTE_LPM_RETURN_IF_TRUE(cond, retval)
83 #endif
84 
86 #define RTE_LPM_VALID_EXT_ENTRY_BITMASK 0x0300
87 
89 #define RTE_LPM_LOOKUP_SUCCESS 0x0100
90 
91 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
92 
93 struct rte_lpm_tbl24_entry {
94  /* Stores Next hop or group index (i.e. gindex)into tbl8. */
95  union {
96  uint8_t next_hop;
97  uint8_t tbl8_gindex;
98  };
99  /* Using single uint8_t to store 3 values. */
100  uint8_t valid :1;
101  uint8_t ext_entry :1;
102  uint8_t depth :6;
103 };
104 
106 struct rte_lpm_tbl8_entry {
107  uint8_t next_hop;
108  /* Using single uint8_t to store 3 values. */
109  uint8_t valid :1;
110  uint8_t valid_group :1;
111  uint8_t depth :6;
112 };
113 #else
114 struct rte_lpm_tbl24_entry {
115  uint8_t depth :6;
116  uint8_t ext_entry :1;
117  uint8_t valid :1;
118  union {
119  uint8_t tbl8_gindex;
120  uint8_t next_hop;
121  };
122 };
123 
124 struct rte_lpm_tbl8_entry {
125  uint8_t depth :6;
126  uint8_t valid_group :1;
127  uint8_t valid :1;
128  uint8_t next_hop;
129 };
130 #endif
131 
133 struct rte_lpm_rule {
134  uint32_t ip;
135  uint8_t next_hop;
136 };
137 
139 struct rte_lpm_rule_info {
140  uint32_t used_rules;
141  uint32_t first_rule;
142 };
143 
145 struct rte_lpm {
146  /* LPM metadata. */
147  char name[RTE_LPM_NAMESIZE];
148  uint32_t max_rules;
149  struct rte_lpm_rule_info rule_info[RTE_LPM_MAX_DEPTH];
151  /* LPM Tables. */
152  struct rte_lpm_tbl24_entry tbl24[RTE_LPM_TBL24_NUM_ENTRIES] \
153  __rte_cache_aligned;
154  struct rte_lpm_tbl8_entry tbl8[RTE_LPM_TBL8_NUM_ENTRIES] \
155  __rte_cache_aligned;
156  struct rte_lpm_rule rules_tbl[0] \
157  __rte_cache_aligned;
158 };
159 
181 struct rte_lpm *
182 rte_lpm_create(const char *name, int socket_id, int max_rules, int flags);
183 
194 struct rte_lpm *
195 rte_lpm_find_existing(const char *name);
196 
205 void
206 rte_lpm_free(struct rte_lpm *lpm);
207 
222 int
223 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t next_hop);
224 
240 int
241 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
242 uint8_t *next_hop);
243 
256 int
257 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth);
258 
265 void
266 rte_lpm_delete_all(struct rte_lpm *lpm);
267 
280 static inline int
281 rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
282 {
283  unsigned tbl24_index = (ip >> 8);
284  uint16_t tbl_entry;
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  memcpy(&tbl_entry, &lpm->tbl24[tbl24_index], sizeof(uint16_t));
291 
292  /* Copy tbl8 entry (only if needed) */
293  if (unlikely((tbl_entry & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
294  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
295 
296  unsigned tbl8_index = (uint8_t)ip +
297  ((uint8_t)tbl_entry * RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
298 
299  memcpy(&tbl_entry, &lpm->tbl8[tbl8_index], sizeof(uint16_t));
300  }
301 
302  *next_hop = (uint8_t)tbl_entry;
303  return (tbl_entry & RTE_LPM_LOOKUP_SUCCESS) ? 0 : -ENOENT;
304 }
305 
326 #define rte_lpm_lookup_bulk(lpm, ips, next_hops, n) \
327  rte_lpm_lookup_bulk_func(lpm, ips, next_hops, n)
328 
329 static inline int
330 rte_lpm_lookup_bulk_func(const struct rte_lpm *lpm, const uint32_t * ips,
331  uint16_t * next_hops, const unsigned n)
332 {
333  unsigned i;
334  unsigned tbl24_indexes[n];
335 
336  /* DEBUG: Check user input arguments. */
337  RTE_LPM_RETURN_IF_TRUE(((lpm == NULL) || (ips == NULL) ||
338  (next_hops == NULL)), -EINVAL);
339 
340  for (i = 0; i < n; i++) {
341  tbl24_indexes[i] = ips[i] >> 8;
342  }
343 
344  for (i = 0; i < n; i++) {
345  /* Simply copy tbl24 entry to output */
346  memcpy(&next_hops[i], &lpm->tbl24[tbl24_indexes[i]], sizeof(uint16_t));
347 
348  /* Overwrite output with tbl8 entry if needed */
349  if (unlikely((next_hops[i] & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
350  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
351 
352  unsigned tbl8_index = (uint8_t)ips[i] +
353  ((uint8_t)next_hops[i] *
354  RTE_LPM_TBL8_GROUP_NUM_ENTRIES);
355 
356  memcpy(&next_hops[i], &lpm->tbl8[tbl8_index], sizeof(uint16_t));
357  }
358  }
359  return 0;
360 }
361 
362 /* Mask four results. */
363 #define RTE_LPM_MASKX4_RES UINT64_C(0x00ff00ff00ff00ff)
364 
384 static inline void
385 rte_lpm_lookupx4(const struct rte_lpm *lpm, __m128i ip, uint16_t hop[4],
386  uint16_t defv)
387 {
388  __m128i i24;
389  rte_xmm_t i8;
390  uint16_t tbl[4];
391  uint64_t idx, pt;
392 
393  const __m128i mask8 =
394  _mm_set_epi32(UINT8_MAX, UINT8_MAX, UINT8_MAX, UINT8_MAX);
395 
396  /*
397  * RTE_LPM_VALID_EXT_ENTRY_BITMASK for 4 LPM entries
398  * as one 64-bit value (0x0300030003000300).
399  */
400  const uint64_t mask_xv =
401  ((uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK |
402  (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 16 |
403  (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 32 |
404  (uint64_t)RTE_LPM_VALID_EXT_ENTRY_BITMASK << 48);
405 
406  /*
407  * RTE_LPM_LOOKUP_SUCCESS for 4 LPM entries
408  * as one 64-bit value (0x0100010001000100).
409  */
410  const uint64_t mask_v =
411  ((uint64_t)RTE_LPM_LOOKUP_SUCCESS |
412  (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 16 |
413  (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 32 |
414  (uint64_t)RTE_LPM_LOOKUP_SUCCESS << 48);
415 
416  /* get 4 indexes for tbl24[]. */
417  i24 = _mm_srli_epi32(ip, CHAR_BIT);
418 
419  /* extract values from tbl24[] */
420  idx = _mm_cvtsi128_si64(i24);
421  i24 = _mm_srli_si128(i24, sizeof(uint64_t));
422 
423  memcpy(&tbl[0], &lpm->tbl24[(uint32_t)idx], sizeof(uint16_t));
424  memcpy(&tbl[1], &lpm->tbl24[idx >> 32], sizeof(uint16_t));
425 
426  idx = _mm_cvtsi128_si64(i24);
427 
428  memcpy(&tbl[2], &lpm->tbl24[(uint32_t)idx], sizeof(uint16_t));
429  memcpy(&tbl[3], &lpm->tbl24[idx >> 32], sizeof(uint16_t));
430 
431  /* get 4 indexes for tbl8[]. */
432  i8.x = _mm_and_si128(ip, mask8);
433 
434  pt = (uint64_t)tbl[0] |
435  (uint64_t)tbl[1] << 16 |
436  (uint64_t)tbl[2] << 32 |
437  (uint64_t)tbl[3] << 48;
438 
439  /* search successfully finished for all 4 IP addresses. */
440  if (likely((pt & mask_xv) == mask_v)) {
441  uintptr_t ph = (uintptr_t)hop;
442  *(uint64_t *)ph = pt & RTE_LPM_MASKX4_RES;
443  return;
444  }
445 
446  if (unlikely((pt & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
447  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
448  i8.u32[0] = i8.u32[0] +
449  (uint8_t)tbl[0] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
450  memcpy(&tbl[0], &lpm->tbl8[i8.u32[0]], sizeof(uint16_t));
451  }
452  if (unlikely((pt >> 16 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
453  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
454  i8.u32[1] = i8.u32[1] +
455  (uint8_t)tbl[1] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
456  memcpy(&tbl[1], &lpm->tbl8[i8.u32[1]], sizeof(uint16_t));
457  }
458  if (unlikely((pt >> 32 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
459  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
460  i8.u32[2] = i8.u32[2] +
461  (uint8_t)tbl[2] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
462  memcpy(&tbl[2], &lpm->tbl8[i8.u32[2]], sizeof(uint16_t));
463  }
464  if (unlikely((pt >> 48 & RTE_LPM_VALID_EXT_ENTRY_BITMASK) ==
465  RTE_LPM_VALID_EXT_ENTRY_BITMASK)) {
466  i8.u32[3] = i8.u32[3] +
467  (uint8_t)tbl[3] * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
468  memcpy(&tbl[3], &lpm->tbl8[i8.u32[3]], sizeof(uint16_t));
469  }
470 
471  hop[0] = (tbl[0] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[0] : defv;
472  hop[1] = (tbl[1] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[1] : defv;
473  hop[2] = (tbl[2] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[2] : defv;
474  hop[3] = (tbl[3] & RTE_LPM_LOOKUP_SUCCESS) ? (uint8_t)tbl[3] : defv;
475 }
476 
477 #ifdef __cplusplus
478 }
479 #endif
480 
481 #endif /* _RTE_LPM_H_ */
#define likely(x)
void rte_lpm_free(struct rte_lpm *lpm)
void rte_lpm_delete_all(struct rte_lpm *lpm)
#define unlikely(x)
#define RTE_LPM_NAMESIZE
Definition: rte_lpm.h:58
int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t next_hop)
static int rte_lpm_lookup(struct rte_lpm *lpm, uint32_t ip, uint8_t *next_hop)
Definition: rte_lpm.h:281
static void rte_lpm_lookupx4(const struct rte_lpm *lpm, __m128i ip, uint16_t hop[4], uint16_t defv)
Definition: rte_lpm.h:385
int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t *next_hop)
int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
#define RTE_LPM_LOOKUP_SUCCESS
Definition: rte_lpm.h:89
struct rte_lpm * rte_lpm_create(const char *name, int socket_id, int max_rules, int flags)
struct rte_lpm * rte_lpm_find_existing(const char *name)