CNDP  22.08.0
trie.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3  * Copyright (c) 2019-2022 Intel Corporation
4  */
5 
6 #ifndef _TRIE_H_
7 #define _TRIE_H_
8 
13 #include <cne_prefetch.h>
14 #include <cne_branch_prediction.h>
15 #include <stdint.h> // for uint8_t, uint64_t, uint32_t, uint16_t
16 
17 #include "cne_common.h" // for __cne_cache_aligned
18 #include "cne_fib6.h" // for cne_fib6_conf (ptr only), cne_fib6_lookup_...
19 #include "private_fib6.h" // for CNE_FIB6_IPV6_ADDR_SIZE, cne_fib6_lookup_fn_t
20 
21 struct cne_fib6;
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* @internal Total number of tbl24 entries. */
28 #define TRIE_TBL24_NUM_ENT (1 << 24)
29 /* Maximum depth value possible for IPv6 LPM. */
30 #define TRIE_MAX_DEPTH 128
31 /* @internal Number of entries in a tbl8 group. */
32 #define TRIE_TBL8_GRP_NUM_ENT 256ULL
33 /* @internal Total number of tbl8 groups in the tbl8. */
34 #define TRIE_TBL8_NUM_GROUPS 65536
35 /* @internal bitmask with valid and valid_group fields set */
36 #define TRIE_EXT_ENT 1
37 
38 #define BITMAP_SLAB_BIT_SIZE_LOG2 6
39 #define BITMAP_SLAB_BIT_SIZE (1ULL << BITMAP_SLAB_BIT_SIZE_LOG2)
40 #define BITMAP_SLAB_BITMASK (BITMAP_SLAB_BIT_SIZE - 1)
41 
42 struct cne_trie_tbl {
43  uint32_t number_tbl8s;
44  uint32_t rsvd_tbl8s;
45  uint32_t cur_tbl8s;
46  uint64_t def_nh;
47  enum cne_fib_trie_nh_sz nh_sz;
48  uint64_t *tbl8;
49  uint32_t *tbl8_pool;
50  uint32_t tbl8_pool_pos;
51  /* tbl24 table. */
52  __extension__ uint64_t tbl24[0] __cne_cache_aligned;
53 };
54 
55 static inline uint32_t
56 get_tbl24_idx(const uint8_t *ip)
57 {
58  return ip[0] << 16 | ip[1] << 8 | ip[2];
59 }
60 
61 static inline void *
62 get_tbl24_p(struct cne_trie_tbl *dp, const uint8_t *ip, uint8_t nh_sz)
63 {
64  uint32_t tbl24_idx;
65 
66  tbl24_idx = get_tbl24_idx(ip);
67  return (void *)&((uint8_t *)dp->tbl24)[tbl24_idx << nh_sz];
68 }
69 
70 static inline uint8_t
71 bits_in_nh(uint8_t nh_sz)
72 {
73  return 8 * (1 << nh_sz);
74 }
75 
76 static inline uint64_t
77 get_max_nh(uint8_t nh_sz)
78 {
79  return ((1ULL << (bits_in_nh(nh_sz) - 1)) - 1);
80 }
81 
82 static inline uint64_t
83 lookup_msk(uint8_t nh_sz)
84 {
85  return ((1ULL << ((1 << (nh_sz + 3)) - 1)) << 1) - 1;
86 }
87 
88 static inline uint8_t
89 get_psd_idx(uint32_t val, uint8_t nh_sz)
90 {
91  return val & ((1 << (3 - nh_sz)) - 1);
92 }
93 
94 static inline uint32_t
95 get_tbl_pos(uint32_t val, uint8_t nh_sz)
96 {
97  return val >> (3 - nh_sz);
98 }
99 
100 static inline uint64_t
101 get_tbl_val_by_idx(uint64_t *tbl, uint32_t idx, uint8_t nh_sz)
102 {
103  return ((tbl[get_tbl_pos(idx, nh_sz)] >> (get_psd_idx(idx, nh_sz) * bits_in_nh(nh_sz))) &
104  lookup_msk(nh_sz));
105 }
106 
107 static inline void *
108 get_tbl_p_by_idx(uint64_t *tbl, uint64_t idx, uint8_t nh_sz)
109 {
110  return (uint8_t *)tbl + (idx << nh_sz);
111 }
112 
113 static inline int
114 is_entry_extended(uint64_t ent)
115 {
116  return (ent & TRIE_EXT_ENT) == TRIE_EXT_ENT;
117 }
118 
119 #define LOOKUP_FUNC(suffix, type, nh_sz) \
120  static inline void cne_trie_lookup_bulk_##suffix(void *p, \
121  uint8_t ips[][CNE_FIB6_IPV6_ADDR_SIZE], \
122  uint64_t *next_hops, const unsigned int n) \
123  { \
124  struct cne_trie_tbl *dp = (struct cne_trie_tbl *)p; \
125  uint64_t tmp; \
126  uint32_t i, j; \
127  \
128  for (i = 0; i < n; i++) { \
129  tmp = ((type *)dp->tbl24)[get_tbl24_idx(&ips[i][0])]; \
130  j = 3; \
131  while (is_entry_extended(tmp)) { \
132  tmp = ((type *)dp->tbl8)[ips[i][j++] + ((tmp >> 1) * TRIE_TBL8_GRP_NUM_ENT)]; \
133  } \
134  next_hops[i] = tmp >> 1; \
135  } \
136  }
137 LOOKUP_FUNC(2b, uint16_t, 1)
138 LOOKUP_FUNC(4b, uint32_t, 2)
139 LOOKUP_FUNC(8b, uint64_t, 3)
140 
141 void *trie_create(const char *name, struct cne_fib6_conf *conf);
142 
143 void trie_free(void *p);
144 
145 cne_fib6_lookup_fn_t trie_get_lookup_fn(void *p, enum cne_fib6_lookup_type type);
146 
147 int trie_modify(struct cne_fib6 *fib, const uint8_t ip[CNE_FIB6_IPV6_ADDR_SIZE], uint8_t depth,
148  uint64_t next_hop, int op);
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif /* _TRIE_H_ */
#define __cne_cache_aligned
Definition: cne_common.h:379
#define CNE_FIB6_IPV6_ADDR_SIZE
Definition: cne_fib6.h:30
cne_fib_trie_nh_sz
Definition: cne_fib6.h:39
cne_fib6_lookup_type
Definition: cne_fib6.h:42
void(* cne_fib6_lookup_fn_t)(void *fib, uint8_t ips[][CNE_FIB6_IPV6_ADDR_SIZE], uint64_t *next_hops, const unsigned int n)
Definition: private_fib6.h:37