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>
17 #define DIR24_8_TBL24_NUM_ENT (1 << 24)
18 #define DIR24_8_TBL8_GRP_NUM_ENT 256U
19 #define DIR24_8_EXT_ENT 1
20 #define DIR24_8_TBL24_MASK 0xffffff00
22 #define BITMAP_SLAB_BIT_SIZE_LOG2 6
23 #define BITMAP_SLAB_BIT_SIZE (1 << BITMAP_SLAB_BIT_SIZE_LOG2)
24 #define BITMAP_SLAB_BITMASK (BITMAP_SLAB_BIT_SIZE - 1)
27 uint32_t number_tbl8s; /**< Total number of tbl8s */
28 uint32_t rsvd_tbl8s; /**< Number of reserved tbl8s */
29 uint32_t cur_tbl8s; /**< Current number of tbl8s */
30 enum rte_fib_dir24_8_nh_sz nh_sz; /**< Size of nexthop entry */
31 uint64_t def_nh; /**< Default next hop */
32 uint64_t *tbl8; /**< tbl8 table. */
33 uint64_t *tbl8_idxes; /**< bitmap containing free tbl8 idxes*/
35 __extension__ uint64_t tbl24[0] __rte_cache_aligned;
39 get_tbl24_p(struct dir24_8_tbl *dp, uint32_t ip, uint8_t nh_sz)
41 return (void *)&((uint8_t *)dp->tbl24)[(ip &
42 DIR24_8_TBL24_MASK) >> (8 - nh_sz)];
46 bits_in_nh(uint8_t nh_sz)
48 return 8 * (1 << nh_sz);
51 static inline uint64_t
52 get_max_nh(uint8_t nh_sz)
54 return ((1ULL << (bits_in_nh(nh_sz) - 1)) - 1);
57 static inline uint32_t
58 get_tbl24_idx(uint32_t ip)
63 static inline uint32_t
64 get_tbl8_idx(uint32_t res, uint32_t ip)
66 return (res >> 1) * DIR24_8_TBL8_GRP_NUM_ENT + (uint8_t)ip;
69 static inline uint64_t
70 lookup_msk(uint8_t nh_sz)
72 return ((1ULL << ((1 << (nh_sz + 3)) - 1)) << 1) - 1;
76 get_psd_idx(uint32_t val, uint8_t nh_sz)
78 return val & ((1 << (3 - nh_sz)) - 1);
81 static inline uint32_t
82 get_tbl_idx(uint32_t val, uint8_t nh_sz)
84 return val >> (3 - nh_sz);
87 static inline uint64_t
88 get_tbl24(struct dir24_8_tbl *dp, uint32_t ip, uint8_t nh_sz)
90 return ((dp->tbl24[get_tbl_idx(get_tbl24_idx(ip), nh_sz)] >>
91 (get_psd_idx(get_tbl24_idx(ip), nh_sz) *
92 bits_in_nh(nh_sz))) & lookup_msk(nh_sz));
95 static inline uint64_t
96 get_tbl8(struct dir24_8_tbl *dp, uint32_t res, uint32_t ip, uint8_t nh_sz)
98 return ((dp->tbl8[get_tbl_idx(get_tbl8_idx(res, ip), nh_sz)] >>
99 (get_psd_idx(get_tbl8_idx(res, ip), nh_sz) *
100 bits_in_nh(nh_sz))) & lookup_msk(nh_sz));
104 is_entry_extended(uint64_t ent)
106 return (ent & DIR24_8_EXT_ENT) == DIR24_8_EXT_ENT;
109 #define LOOKUP_FUNC(suffix, type, bulk_prefetch, nh_sz) \
110 static inline void dir24_8_lookup_bulk_##suffix(void *p, const uint32_t *ips, \
111 uint64_t *next_hops, const unsigned int n) \
113 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p; \
116 uint32_t prefetch_offset = \
117 RTE_MIN((unsigned int)bulk_prefetch, n); \
119 for (i = 0; i < prefetch_offset; i++) \
120 rte_prefetch0(get_tbl24_p(dp, ips[i], nh_sz)); \
121 for (i = 0; i < (n - prefetch_offset); i++) { \
122 rte_prefetch0(get_tbl24_p(dp, \
123 ips[i + prefetch_offset], nh_sz)); \
124 tmp = ((type *)dp->tbl24)[ips[i] >> 8]; \
125 if (unlikely(is_entry_extended(tmp))) \
126 tmp = ((type *)dp->tbl8)[(uint8_t)ips[i] + \
127 ((tmp >> 1) * DIR24_8_TBL8_GRP_NUM_ENT)]; \
128 next_hops[i] = tmp >> 1; \
130 for (; i < n; i++) { \
131 tmp = ((type *)dp->tbl24)[ips[i] >> 8]; \
132 if (unlikely(is_entry_extended(tmp))) \
133 tmp = ((type *)dp->tbl8)[(uint8_t)ips[i] + \
134 ((tmp >> 1) * DIR24_8_TBL8_GRP_NUM_ENT)]; \
135 next_hops[i] = tmp >> 1; \
139 LOOKUP_FUNC(1b, uint8_t, 5, 0)
140 LOOKUP_FUNC(2b, uint16_t, 6, 1)
141 LOOKUP_FUNC(4b, uint32_t, 15, 2)
142 LOOKUP_FUNC(8b, uint64_t, 12, 3)
145 dir24_8_lookup_bulk(struct dir24_8_tbl *dp, const uint32_t *ips,
146 uint64_t *next_hops, const unsigned int n, uint8_t nh_sz)
150 uint32_t prefetch_offset = RTE_MIN(15U, n);
152 for (i = 0; i < prefetch_offset; i++)
153 rte_prefetch0(get_tbl24_p(dp, ips[i], nh_sz));
154 for (i = 0; i < (n - prefetch_offset); i++) {
155 rte_prefetch0(get_tbl24_p(dp, ips[i + prefetch_offset],
157 tmp = get_tbl24(dp, ips[i], nh_sz);
158 if (unlikely(is_entry_extended(tmp)))
159 tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
161 next_hops[i] = tmp >> 1;
164 tmp = get_tbl24(dp, ips[i], nh_sz);
165 if (unlikely(is_entry_extended(tmp)))
166 tmp = get_tbl8(dp, tmp, ips[i], nh_sz);
168 next_hops[i] = tmp >> 1;
173 dir24_8_lookup_bulk_0(void *p, const uint32_t *ips,
174 uint64_t *next_hops, const unsigned int n)
176 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
178 dir24_8_lookup_bulk(dp, ips, next_hops, n, 0);
182 dir24_8_lookup_bulk_1(void *p, const uint32_t *ips,
183 uint64_t *next_hops, const unsigned int n)
185 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
187 dir24_8_lookup_bulk(dp, ips, next_hops, n, 1);
191 dir24_8_lookup_bulk_2(void *p, const uint32_t *ips,
192 uint64_t *next_hops, const unsigned int n)
194 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
196 dir24_8_lookup_bulk(dp, ips, next_hops, n, 2);
200 dir24_8_lookup_bulk_3(void *p, const uint32_t *ips,
201 uint64_t *next_hops, const unsigned int n)
203 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
205 dir24_8_lookup_bulk(dp, ips, next_hops, n, 3);
209 dir24_8_lookup_bulk_uni(void *p, const uint32_t *ips,
210 uint64_t *next_hops, const unsigned int n)
212 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
215 uint32_t prefetch_offset = RTE_MIN(15U, n);
216 uint8_t nh_sz = dp->nh_sz;
218 for (i = 0; i < prefetch_offset; i++)
219 rte_prefetch0(get_tbl24_p(dp, ips[i], nh_sz));
220 for (i = 0; i < (n - prefetch_offset); i++) {
221 rte_prefetch0(get_tbl24_p(dp, ips[i + prefetch_offset],
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);
227 next_hops[i] = tmp >> 1;
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);
234 next_hops[i] = tmp >> 1;
239 dir24_8_create(const char *name, int socket_id, struct rte_fib_conf *conf);
242 dir24_8_free(void *p);
245 dir24_8_get_lookup_fn(void *p, enum rte_fib_lookup_type type);
248 dir24_8_modify(struct rte_fib *fib, uint32_t ip, uint8_t depth,
249 uint64_t next_hop, int op);
251 #endif /* _DIR24_8_H_ */