1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
9 #include <rte_prefetch.h>
10 #include <rte_branch_prediction.h>
21 #define DIR24_8_TBL24_NUM_ENT (1 << 24)
22 #define DIR24_8_TBL8_GRP_NUM_ENT 256U
23 #define DIR24_8_EXT_ENT 1
24 #define DIR24_8_TBL24_MASK 0xffffff00
26 #define BITMAP_SLAB_BIT_SIZE_LOG2 6
27 #define BITMAP_SLAB_BIT_SIZE (1 << BITMAP_SLAB_BIT_SIZE_LOG2)
28 #define BITMAP_SLAB_BITMASK (BITMAP_SLAB_BIT_SIZE - 1)
31 uint32_t number_tbl8s; /**< Total number of tbl8s */
32 uint32_t rsvd_tbl8s; /**< Number of reserved tbl8s */
33 uint32_t cur_tbl8s; /**< Current number of tbl8s */
34 enum rte_fib_dir24_8_nh_sz nh_sz; /**< Size of nexthop entry */
35 uint64_t def_nh; /**< Default next hop */
36 uint64_t *tbl8; /**< tbl8 table. */
37 uint64_t *tbl8_idxes; /**< bitmap containing free tbl8 idxes*/
39 __extension__ uint64_t tbl24[0] __rte_cache_aligned;
43 get_tbl24_p(struct dir24_8_tbl *dp, uint32_t ip, uint8_t nh_sz)
45 return (void *)&((uint8_t *)dp->tbl24)[(ip &
46 DIR24_8_TBL24_MASK) >> (8 - nh_sz)];
50 bits_in_nh(uint8_t nh_sz)
52 return 8 * (1 << nh_sz);
55 static inline uint64_t
56 get_max_nh(uint8_t nh_sz)
58 return ((1ULL << (bits_in_nh(nh_sz) - 1)) - 1);
61 static inline uint32_t
62 get_tbl24_idx(uint32_t ip)
67 static inline uint32_t
68 get_tbl8_idx(uint32_t res, uint32_t ip)
70 return (res >> 1) * DIR24_8_TBL8_GRP_NUM_ENT + (uint8_t)ip;
73 static inline uint64_t
74 lookup_msk(uint8_t nh_sz)
76 return ((1ULL << ((1 << (nh_sz + 3)) - 1)) << 1) - 1;
80 get_psd_idx(uint32_t val, uint8_t nh_sz)
82 return val & ((1 << (3 - nh_sz)) - 1);
85 static inline uint32_t
86 get_tbl_idx(uint32_t val, uint8_t nh_sz)
88 return val >> (3 - nh_sz);
91 static inline uint64_t
92 get_tbl24(struct dir24_8_tbl *dp, uint32_t ip, uint8_t nh_sz)
94 return ((dp->tbl24[get_tbl_idx(get_tbl24_idx(ip), nh_sz)] >>
95 (get_psd_idx(get_tbl24_idx(ip), nh_sz) *
96 bits_in_nh(nh_sz))) & lookup_msk(nh_sz));
99 static inline uint64_t
100 get_tbl8(struct dir24_8_tbl *dp, uint32_t res, uint32_t ip, uint8_t nh_sz)
102 return ((dp->tbl8[get_tbl_idx(get_tbl8_idx(res, ip), nh_sz)] >>
103 (get_psd_idx(get_tbl8_idx(res, ip), nh_sz) *
104 bits_in_nh(nh_sz))) & lookup_msk(nh_sz));
108 is_entry_extended(uint64_t ent)
110 return (ent & DIR24_8_EXT_ENT) == DIR24_8_EXT_ENT;
113 #define LOOKUP_FUNC(suffix, type, bulk_prefetch, nh_sz) \
114 static inline void dir24_8_lookup_bulk_##suffix(void *p, const uint32_t *ips, \
115 uint64_t *next_hops, const unsigned int n) \
117 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p; \
120 uint32_t prefetch_offset = \
121 RTE_MIN((unsigned int)bulk_prefetch, n); \
123 for (i = 0; i < prefetch_offset; i++) \
124 rte_prefetch0(get_tbl24_p(dp, ips[i], nh_sz)); \
125 for (i = 0; i < (n - prefetch_offset); i++) { \
126 rte_prefetch0(get_tbl24_p(dp, \
127 ips[i + prefetch_offset], nh_sz)); \
128 tmp = ((type *)dp->tbl24)[ips[i] >> 8]; \
129 if (unlikely(is_entry_extended(tmp))) \
130 tmp = ((type *)dp->tbl8)[(uint8_t)ips[i] + \
131 ((tmp >> 1) * DIR24_8_TBL8_GRP_NUM_ENT)]; \
132 next_hops[i] = tmp >> 1; \
134 for (; i < n; i++) { \
135 tmp = ((type *)dp->tbl24)[ips[i] >> 8]; \
136 if (unlikely(is_entry_extended(tmp))) \
137 tmp = ((type *)dp->tbl8)[(uint8_t)ips[i] + \
138 ((tmp >> 1) * DIR24_8_TBL8_GRP_NUM_ENT)]; \
139 next_hops[i] = tmp >> 1; \
143 LOOKUP_FUNC(1b, uint8_t, 5, 0)
144 LOOKUP_FUNC(2b, uint16_t, 6, 1)
145 LOOKUP_FUNC(4b, uint32_t, 15, 2)
146 LOOKUP_FUNC(8b, uint64_t, 12, 3)
149 dir24_8_lookup_bulk(struct dir24_8_tbl *dp, const uint32_t *ips,
150 uint64_t *next_hops, const unsigned int n, uint8_t nh_sz)
154 uint32_t prefetch_offset = RTE_MIN(15U, n);
156 for (i = 0; i < prefetch_offset; i++)
157 rte_prefetch0(get_tbl24_p(dp, ips[i], nh_sz));
158 for (i = 0; i < (n - prefetch_offset); i++) {
159 rte_prefetch0(get_tbl24_p(dp, ips[i + prefetch_offset],
161 tmp = get_tbl24(dp, ips[i], nh_sz);
162 if (unlikely(is_entry_extended(tmp)))
163 tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
165 next_hops[i] = tmp >> 1;
168 tmp = get_tbl24(dp, ips[i], nh_sz);
169 if (unlikely(is_entry_extended(tmp)))
170 tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
172 next_hops[i] = tmp >> 1;
177 dir24_8_lookup_bulk_0(void *p, const uint32_t *ips,
178 uint64_t *next_hops, const unsigned int n)
180 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
182 dir24_8_lookup_bulk(dp, ips, next_hops, n, 0);
186 dir24_8_lookup_bulk_1(void *p, const uint32_t *ips,
187 uint64_t *next_hops, const unsigned int n)
189 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
191 dir24_8_lookup_bulk(dp, ips, next_hops, n, 1);
195 dir24_8_lookup_bulk_2(void *p, const uint32_t *ips,
196 uint64_t *next_hops, const unsigned int n)
198 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
200 dir24_8_lookup_bulk(dp, ips, next_hops, n, 2);
204 dir24_8_lookup_bulk_3(void *p, const uint32_t *ips,
205 uint64_t *next_hops, const unsigned int n)
207 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
209 dir24_8_lookup_bulk(dp, ips, next_hops, n, 3);
213 dir24_8_lookup_bulk_uni(void *p, const uint32_t *ips,
214 uint64_t *next_hops, const unsigned int n)
216 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
219 uint32_t prefetch_offset = RTE_MIN(15U, n);
220 uint8_t nh_sz = dp->nh_sz;
222 for (i = 0; i < prefetch_offset; i++)
223 rte_prefetch0(get_tbl24_p(dp, ips[i], nh_sz));
224 for (i = 0; i < (n - prefetch_offset); i++) {
225 rte_prefetch0(get_tbl24_p(dp, ips[i + prefetch_offset],
227 tmp = get_tbl24(dp, ips[i], nh_sz);
228 if (unlikely(is_entry_extended(tmp)))
229 tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
231 next_hops[i] = tmp >> 1;
234 tmp = get_tbl24(dp, ips[i], nh_sz);
235 if (unlikely(is_entry_extended(tmp)))
236 tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
238 next_hops[i] = tmp >> 1;
243 dir24_8_create(const char *name, int socket_id, struct rte_fib_conf *conf);
246 dir24_8_free(void *p);
249 dir24_8_get_lookup_fn(void *p, enum rte_fib_lookup_type type);
252 dir24_8_modify(struct rte_fib *fib, uint32_t ip, uint8_t depth,
253 uint64_t next_hop, int op);
259 #endif /* _DIR24_8_H_ */