CNDP  22.08.0
dir24_8.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 _DIR24_8_H_
7 #define _DIR24_8_H_
8 
9 #include <cne_prefetch.h> // for cne_prefetch0
10 #include <cne_branch_prediction.h> // for unlikely
11 #include <stdint.h> // for uint32_t, uint64_t, uint8_t, uint...
12 
13 #include "cne_common.h" // for CNE_MIN, __cne_cache_aligned
14 #include "cne_fib.h" // for cne_fib_conf (ptr only), cne_fib_...
15 
16 struct cne_fib;
17 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define DIR24_8_TBL24_NUM_ENT (1 << 24)
28 #define DIR24_8_TBL8_GRP_NUM_ENT 256U
29 #define DIR24_8_EXT_ENT 1
30 #define DIR24_8_TBL24_MASK 0xffffff00
31 
32 #define BITMAP_SLAB_BIT_SIZE_LOG2 6
33 #define BITMAP_SLAB_BIT_SIZE (1 << BITMAP_SLAB_BIT_SIZE_LOG2)
34 #define BITMAP_SLAB_BITMASK (BITMAP_SLAB_BIT_SIZE - 1)
35 
36 struct dir24_8_tbl {
37  uint32_t number_tbl8s;
38  uint32_t rsvd_tbl8s;
39  uint32_t cur_tbl8s;
40  enum cne_fib_dir24_8_nh_sz nh_sz;
41  uint64_t def_nh;
42  uint64_t *tbl8;
43  uint64_t *tbl8_idxes;
44  /* tbl24 table. */
45  __extension__ uint64_t tbl24[0] __cne_cache_aligned;
46 };
47 
48 static inline void *
49 get_tbl24_p(struct dir24_8_tbl *dp, uint32_t ip, uint8_t nh_sz)
50 {
51  return (void *)&((uint8_t *)dp->tbl24)[(ip & DIR24_8_TBL24_MASK) >> (8 - nh_sz)];
52 }
53 
54 static inline uint8_t
55 bits_in_nh(uint8_t nh_sz)
56 {
57  return 8 * (1 << nh_sz);
58 }
59 
60 static inline uint64_t
61 get_max_nh(uint8_t nh_sz)
62 {
63  return ((1ULL << (bits_in_nh(nh_sz) - 1)) - 1);
64 }
65 
66 static inline uint32_t
67 get_tbl24_idx(uint32_t ip)
68 {
69  return ip >> 8;
70 }
71 
72 static inline uint32_t
73 get_tbl8_idx(uint32_t res, uint32_t ip)
74 {
75  return (res >> 1) * DIR24_8_TBL8_GRP_NUM_ENT + (uint8_t)ip;
76 }
77 
78 static inline uint64_t
79 lookup_msk(uint8_t nh_sz)
80 {
81  return ((1ULL << ((1 << (nh_sz + 3)) - 1)) << 1) - 1;
82 }
83 
84 static inline uint8_t
85 get_psd_idx(uint32_t val, uint8_t nh_sz)
86 {
87  return val & ((1 << (3 - nh_sz)) - 1);
88 }
89 
90 static inline uint32_t
91 get_tbl_idx(uint32_t val, uint8_t nh_sz)
92 {
93  return val >> (3 - nh_sz);
94 }
95 
96 static inline uint64_t
97 get_tbl24(struct dir24_8_tbl *dp, uint32_t ip, uint8_t nh_sz)
98 {
99  return ((dp->tbl24[get_tbl_idx(get_tbl24_idx(ip), nh_sz)] >>
100  (get_psd_idx(get_tbl24_idx(ip), nh_sz) * bits_in_nh(nh_sz))) &
101  lookup_msk(nh_sz));
102 }
103 
104 static inline uint64_t
105 get_tbl8(struct dir24_8_tbl *dp, uint32_t res, uint32_t ip, uint8_t nh_sz)
106 {
107  return ((dp->tbl8[get_tbl_idx(get_tbl8_idx(res, ip), nh_sz)] >>
108  (get_psd_idx(get_tbl8_idx(res, ip), nh_sz) * bits_in_nh(nh_sz))) &
109  lookup_msk(nh_sz));
110 }
111 
112 static inline int
113 is_entry_extended(uint64_t ent)
114 {
115  return (ent & DIR24_8_EXT_ENT) == DIR24_8_EXT_ENT;
116 }
117 
118 #define LOOKUP_FUNC(suffix, type, bulk_prefetch, nh_sz) \
119  static inline void dir24_8_lookup_bulk_##suffix(void *p, const uint32_t *ips, \
120  uint64_t *next_hops, const unsigned int n) \
121  { \
122  struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p; \
123  uint64_t tmp; \
124  uint32_t i; \
125  uint32_t prefetch_offset = CNE_MIN((unsigned int)bulk_prefetch, n); \
126  \
127  for (i = 0; i < prefetch_offset; i++) \
128  cne_prefetch0(get_tbl24_p(dp, ips[i], nh_sz)); \
129  for (i = 0; i < (n - prefetch_offset); i++) { \
130  cne_prefetch0(get_tbl24_p(dp, ips[i + prefetch_offset], nh_sz)); \
131  tmp = ((type *)dp->tbl24)[ips[i] >> 8]; \
132  if (unlikely(is_entry_extended(tmp))) \
133  tmp = \
134  ((type *)dp->tbl8)[(uint8_t)ips[i] + ((tmp >> 1) * DIR24_8_TBL8_GRP_NUM_ENT)]; \
135  next_hops[i] = tmp >> 1; \
136  } \
137  for (; i < n; i++) { \
138  tmp = ((type *)dp->tbl24)[ips[i] >> 8]; \
139  if (unlikely(is_entry_extended(tmp))) \
140  tmp = \
141  ((type *)dp->tbl8)[(uint8_t)ips[i] + ((tmp >> 1) * DIR24_8_TBL8_GRP_NUM_ENT)]; \
142  next_hops[i] = tmp >> 1; \
143  } \
144  }
145 
146 LOOKUP_FUNC(1b, uint8_t, 5, 0)
147 LOOKUP_FUNC(2b, uint16_t, 6, 1)
148 LOOKUP_FUNC(4b, uint32_t, 15, 2)
149 LOOKUP_FUNC(8b, uint64_t, 12, 3)
150 
151 static inline void
152 dir24_8_lookup_bulk(struct dir24_8_tbl *dp, const uint32_t *ips, uint64_t *next_hops,
153  const unsigned int n, uint8_t nh_sz)
154 {
155  uint64_t tmp;
156  uint32_t i;
157  uint32_t prefetch_offset = CNE_MIN(15U, n);
158 
159  for (i = 0; i < prefetch_offset; i++)
160  cne_prefetch0(get_tbl24_p(dp, ips[i], nh_sz));
161  for (i = 0; i < (n - prefetch_offset); i++) {
162  cne_prefetch0(get_tbl24_p(dp, ips[i + prefetch_offset], nh_sz));
163  tmp = get_tbl24(dp, ips[i], nh_sz);
164  if (unlikely(is_entry_extended(tmp)))
165  tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
166 
167  next_hops[i] = tmp >> 1;
168  }
169  for (; i < n; i++) {
170  tmp = get_tbl24(dp, ips[i], nh_sz);
171  if (unlikely(is_entry_extended(tmp)))
172  tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
173 
174  next_hops[i] = tmp >> 1;
175  }
176 }
177 
178 static inline void
179 dir24_8_lookup_bulk_0(void *p, const uint32_t *ips, uint64_t *next_hops, const unsigned int n)
180 {
181  struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
182 
183  dir24_8_lookup_bulk(dp, ips, next_hops, n, 0);
184 }
185 
186 static inline void
187 dir24_8_lookup_bulk_1(void *p, const uint32_t *ips, uint64_t *next_hops, const unsigned int n)
188 {
189  struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
190 
191  dir24_8_lookup_bulk(dp, ips, next_hops, n, 1);
192 }
193 
194 static inline void
195 dir24_8_lookup_bulk_2(void *p, const uint32_t *ips, uint64_t *next_hops, const unsigned int n)
196 {
197  struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
198 
199  dir24_8_lookup_bulk(dp, ips, next_hops, n, 2);
200 }
201 
202 static inline void
203 dir24_8_lookup_bulk_3(void *p, const uint32_t *ips, uint64_t *next_hops, const unsigned int n)
204 {
205  struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
206 
207  dir24_8_lookup_bulk(dp, ips, next_hops, n, 3);
208 }
209 
210 static inline void
211 dir24_8_lookup_bulk_uni(void *p, const uint32_t *ips, uint64_t *next_hops, const unsigned int n)
212 {
213  struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
214  uint64_t tmp;
215  uint32_t i;
216  uint32_t prefetch_offset = CNE_MIN(15U, n);
217  uint8_t nh_sz = dp->nh_sz;
218 
219  for (i = 0; i < prefetch_offset; i++)
220  cne_prefetch0(get_tbl24_p(dp, ips[i], nh_sz));
221  for (i = 0; i < (n - prefetch_offset); i++) {
222  cne_prefetch0(get_tbl24_p(dp, ips[i + prefetch_offset], nh_sz));
223  tmp = get_tbl24(dp, ips[i], nh_sz);
224  if (unlikely(is_entry_extended(tmp)))
225  tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
226 
227  next_hops[i] = tmp >> 1;
228  }
229  for (; i < n; i++) {
230  tmp = get_tbl24(dp, ips[i], nh_sz);
231  if (unlikely(is_entry_extended(tmp)))
232  tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
233 
234  next_hops[i] = tmp >> 1;
235  }
236 }
237 
238 void *dir24_8_create(struct cne_fib_conf *conf);
239 
240 void dir24_8_free(void *p);
241 
242 cne_fib_lookup_fn_t dir24_8_get_lookup_fn(void *p, enum cne_fib_lookup_type type);
243 
244 int dir24_8_modify(struct cne_fib *fib, uint32_t ip, uint8_t depth, uint64_t next_hop, int op);
245 
246 #ifdef __cplusplus
247 }
248 #endif
249 
250 #endif /* _DIR24_8_H_ */
#define unlikely(x)
#define CNE_MIN(a, b)
Definition: cne_common.h:550
#define __cne_cache_aligned
Definition: cne_common.h:379
void(* cne_fib_lookup_fn_t)(void *fib, const uint32_t *ips, uint64_t *next_hops, const unsigned int n)
Definition: cne_fib.h:41
cne_fib_lookup_type
Definition: cne_fib.h:58
cne_fib_dir24_8_nh_sz
Definition: cne_fib.h:50
static void cne_prefetch0(const volatile void *p)
Definition: cne_prefetch.h:27