1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
12 #include <rte_debug.h>
13 #include <rte_malloc.h>
14 #include <rte_errno.h>
15 #include <rte_memory.h>
22 #ifdef CC_DIR24_8_AVX512_SUPPORT
24 #include "dir24_8_avx512.h"
26 #endif /* CC_DIR24_8_AVX512_SUPPORT */
28 #define DIR24_8_NAMESIZE 64
30 #define ROUNDUP(x, y) RTE_ALIGN_CEIL(x, (1 << (32 - y)))
32 static inline rte_fib_lookup_fn_t
33 get_scalar_fn(enum rte_fib_dir24_8_nh_sz nh_sz)
36 case RTE_FIB_DIR24_8_1B:
37 return dir24_8_lookup_bulk_1b;
38 case RTE_FIB_DIR24_8_2B:
39 return dir24_8_lookup_bulk_2b;
40 case RTE_FIB_DIR24_8_4B:
41 return dir24_8_lookup_bulk_4b;
42 case RTE_FIB_DIR24_8_8B:
43 return dir24_8_lookup_bulk_8b;
49 static inline rte_fib_lookup_fn_t
50 get_scalar_fn_inlined(enum rte_fib_dir24_8_nh_sz nh_sz)
53 case RTE_FIB_DIR24_8_1B:
54 return dir24_8_lookup_bulk_0;
55 case RTE_FIB_DIR24_8_2B:
56 return dir24_8_lookup_bulk_1;
57 case RTE_FIB_DIR24_8_4B:
58 return dir24_8_lookup_bulk_2;
59 case RTE_FIB_DIR24_8_8B:
60 return dir24_8_lookup_bulk_3;
66 static inline rte_fib_lookup_fn_t
67 get_vector_fn(enum rte_fib_dir24_8_nh_sz nh_sz)
69 #ifdef CC_DIR24_8_AVX512_SUPPORT
70 if ((rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) <= 0) ||
71 (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_512))
75 case RTE_FIB_DIR24_8_1B:
76 return rte_dir24_8_vec_lookup_bulk_1b;
77 case RTE_FIB_DIR24_8_2B:
78 return rte_dir24_8_vec_lookup_bulk_2b;
79 case RTE_FIB_DIR24_8_4B:
80 return rte_dir24_8_vec_lookup_bulk_4b;
81 case RTE_FIB_DIR24_8_8B:
82 return rte_dir24_8_vec_lookup_bulk_8b;
93 dir24_8_get_lookup_fn(void *p, enum rte_fib_lookup_type type)
95 enum rte_fib_dir24_8_nh_sz nh_sz;
96 rte_fib_lookup_fn_t ret_fn;
97 struct dir24_8_tbl *dp = p;
105 case RTE_FIB_LOOKUP_DIR24_8_SCALAR_MACRO:
106 return get_scalar_fn(nh_sz);
107 case RTE_FIB_LOOKUP_DIR24_8_SCALAR_INLINE:
108 return get_scalar_fn_inlined(nh_sz);
109 case RTE_FIB_LOOKUP_DIR24_8_SCALAR_UNI:
110 return dir24_8_lookup_bulk_uni;
111 case RTE_FIB_LOOKUP_DIR24_8_VECTOR_AVX512:
112 return get_vector_fn(nh_sz);
113 case RTE_FIB_LOOKUP_DEFAULT:
114 ret_fn = get_vector_fn(nh_sz);
115 return (ret_fn != NULL) ? ret_fn : get_scalar_fn(nh_sz);
124 write_to_fib(void *ptr, uint64_t val, enum rte_fib_dir24_8_nh_sz size, int n)
127 uint8_t *ptr8 = (uint8_t *)ptr;
128 uint16_t *ptr16 = (uint16_t *)ptr;
129 uint32_t *ptr32 = (uint32_t *)ptr;
130 uint64_t *ptr64 = (uint64_t *)ptr;
133 case RTE_FIB_DIR24_8_1B:
134 for (i = 0; i < n; i++)
135 ptr8[i] = (uint8_t)val;
137 case RTE_FIB_DIR24_8_2B:
138 for (i = 0; i < n; i++)
139 ptr16[i] = (uint16_t)val;
141 case RTE_FIB_DIR24_8_4B:
142 for (i = 0; i < n; i++)
143 ptr32[i] = (uint32_t)val;
145 case RTE_FIB_DIR24_8_8B:
146 for (i = 0; i < n; i++)
147 ptr64[i] = (uint64_t)val;
153 tbl8_get_idx(struct dir24_8_tbl *dp)
158 for (i = 0; (i < (dp->number_tbl8s >> BITMAP_SLAB_BIT_SIZE_LOG2)) &&
159 (dp->tbl8_idxes[i] == UINT64_MAX); i++)
161 if (i < (dp->number_tbl8s >> BITMAP_SLAB_BIT_SIZE_LOG2)) {
162 bit_idx = __builtin_ctzll(~dp->tbl8_idxes[i]);
163 dp->tbl8_idxes[i] |= (1ULL << bit_idx);
164 return (i << BITMAP_SLAB_BIT_SIZE_LOG2) + bit_idx;
170 tbl8_free_idx(struct dir24_8_tbl *dp, int idx)
172 dp->tbl8_idxes[idx >> BITMAP_SLAB_BIT_SIZE_LOG2] &=
173 ~(1ULL << (idx & BITMAP_SLAB_BITMASK));
177 tbl8_alloc(struct dir24_8_tbl *dp, uint64_t nh)
182 tbl8_idx = tbl8_get_idx(dp);
185 tbl8_ptr = (uint8_t *)dp->tbl8 +
186 ((tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT) <<
188 /*Init tbl8 entries with nexthop from tbl24*/
189 write_to_fib((void *)tbl8_ptr, nh|
190 DIR24_8_EXT_ENT, dp->nh_sz,
191 DIR24_8_TBL8_GRP_NUM_ENT);
197 tbl8_recycle(struct dir24_8_tbl *dp, uint32_t ip, uint64_t tbl8_idx)
207 case RTE_FIB_DIR24_8_1B:
208 ptr8 = &((uint8_t *)dp->tbl8)[tbl8_idx *
209 DIR24_8_TBL8_GRP_NUM_ENT];
211 for (i = 1; i < DIR24_8_TBL8_GRP_NUM_ENT; i++) {
215 ((uint8_t *)dp->tbl24)[ip >> 8] =
216 nh & ~DIR24_8_EXT_ENT;
217 for (i = 0; i < DIR24_8_TBL8_GRP_NUM_ENT; i++)
220 case RTE_FIB_DIR24_8_2B:
221 ptr16 = &((uint16_t *)dp->tbl8)[tbl8_idx *
222 DIR24_8_TBL8_GRP_NUM_ENT];
224 for (i = 1; i < DIR24_8_TBL8_GRP_NUM_ENT; i++) {
228 ((uint16_t *)dp->tbl24)[ip >> 8] =
229 nh & ~DIR24_8_EXT_ENT;
230 for (i = 0; i < DIR24_8_TBL8_GRP_NUM_ENT; i++)
233 case RTE_FIB_DIR24_8_4B:
234 ptr32 = &((uint32_t *)dp->tbl8)[tbl8_idx *
235 DIR24_8_TBL8_GRP_NUM_ENT];
237 for (i = 1; i < DIR24_8_TBL8_GRP_NUM_ENT; i++) {
241 ((uint32_t *)dp->tbl24)[ip >> 8] =
242 nh & ~DIR24_8_EXT_ENT;
243 for (i = 0; i < DIR24_8_TBL8_GRP_NUM_ENT; i++)
246 case RTE_FIB_DIR24_8_8B:
247 ptr64 = &((uint64_t *)dp->tbl8)[tbl8_idx *
248 DIR24_8_TBL8_GRP_NUM_ENT];
250 for (i = 1; i < DIR24_8_TBL8_GRP_NUM_ENT; i++) {
254 ((uint64_t *)dp->tbl24)[ip >> 8] =
255 nh & ~DIR24_8_EXT_ENT;
256 for (i = 0; i < DIR24_8_TBL8_GRP_NUM_ENT; i++)
260 tbl8_free_idx(dp, tbl8_idx);
265 install_to_fib(struct dir24_8_tbl *dp, uint32_t ledge, uint32_t redge,
274 len = ((ledge == 0) && (redge == 0)) ? 1 << 24 :
275 ((redge & DIR24_8_TBL24_MASK) - ROUNDUP(ledge, 24)) >> 8;
277 if (((ledge >> 8) != (redge >> 8)) || (len == 1 << 24)) {
278 if ((ROUNDUP(ledge, 24) - ledge) != 0) {
279 tbl24_tmp = get_tbl24(dp, ledge, dp->nh_sz);
280 if ((tbl24_tmp & DIR24_8_EXT_ENT) !=
283 * Make sure there is space for two TBL8.
284 * This is necessary when installing range that
285 * needs tbl8 for ledge and redge.
287 tbl8_idx = tbl8_alloc(dp, tbl24_tmp);
288 tmp_tbl8_idx = tbl8_get_idx(dp);
291 else if (tmp_tbl8_idx < 0) {
292 tbl8_free_idx(dp, tbl8_idx);
295 tbl8_free_idx(dp, tmp_tbl8_idx);
296 /*update dir24 entry with tbl8 index*/
297 write_to_fib(get_tbl24_p(dp, ledge,
298 dp->nh_sz), (tbl8_idx << 1)|
302 tbl8_idx = tbl24_tmp >> 1;
303 tbl8_ptr = (uint8_t *)dp->tbl8 +
304 (((tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT) +
305 (ledge & ~DIR24_8_TBL24_MASK)) <<
307 /*update tbl8 with new next hop*/
308 write_to_fib((void *)tbl8_ptr, (next_hop << 1)|
310 dp->nh_sz, ROUNDUP(ledge, 24) - ledge);
311 tbl8_recycle(dp, ledge, tbl8_idx);
313 write_to_fib(get_tbl24_p(dp, ROUNDUP(ledge, 24), dp->nh_sz),
314 next_hop << 1, dp->nh_sz, len);
315 if (redge & ~DIR24_8_TBL24_MASK) {
316 tbl24_tmp = get_tbl24(dp, redge, dp->nh_sz);
317 if ((tbl24_tmp & DIR24_8_EXT_ENT) !=
319 tbl8_idx = tbl8_alloc(dp, tbl24_tmp);
322 /*update dir24 entry with tbl8 index*/
323 write_to_fib(get_tbl24_p(dp, redge,
324 dp->nh_sz), (tbl8_idx << 1)|
328 tbl8_idx = tbl24_tmp >> 1;
329 tbl8_ptr = (uint8_t *)dp->tbl8 +
330 ((tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT) <<
332 /*update tbl8 with new next hop*/
333 write_to_fib((void *)tbl8_ptr, (next_hop << 1)|
335 dp->nh_sz, redge & ~DIR24_8_TBL24_MASK);
336 tbl8_recycle(dp, redge, tbl8_idx);
338 } else if ((redge - ledge) != 0) {
339 tbl24_tmp = get_tbl24(dp, ledge, dp->nh_sz);
340 if ((tbl24_tmp & DIR24_8_EXT_ENT) !=
342 tbl8_idx = tbl8_alloc(dp, tbl24_tmp);
345 /*update dir24 entry with tbl8 index*/
346 write_to_fib(get_tbl24_p(dp, ledge, dp->nh_sz),
351 tbl8_idx = tbl24_tmp >> 1;
352 tbl8_ptr = (uint8_t *)dp->tbl8 +
353 (((tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT) +
354 (ledge & ~DIR24_8_TBL24_MASK)) <<
356 /*update tbl8 with new next hop*/
357 write_to_fib((void *)tbl8_ptr, (next_hop << 1)|
359 dp->nh_sz, redge - ledge);
360 tbl8_recycle(dp, ledge, tbl8_idx);
366 modify_fib(struct dir24_8_tbl *dp, struct rte_rib *rib, uint32_t ip,
367 uint8_t depth, uint64_t next_hop)
369 struct rte_rib_node *tmp = NULL;
370 uint32_t ledge, redge, tmp_ip;
376 tmp = rte_rib_get_nxt(rib, ip, depth, tmp,
377 RTE_RIB_GET_NXT_COVER);
379 rte_rib_get_depth(tmp, &tmp_depth);
380 if (tmp_depth == depth)
382 rte_rib_get_ip(tmp, &tmp_ip);
383 redge = tmp_ip & rte_rib_depth_to_mask(tmp_depth);
384 if (ledge == redge) {
386 (uint32_t)(1ULL << (32 - tmp_depth));
389 ret = install_to_fib(dp, ledge, redge,
394 (uint32_t)(1ULL << (32 - tmp_depth));
396 redge = ip + (uint32_t)(1ULL << (32 - depth));
399 ret = install_to_fib(dp, ledge, redge,
410 dir24_8_modify(struct rte_fib *fib, uint32_t ip, uint8_t depth,
411 uint64_t next_hop, int op)
413 struct dir24_8_tbl *dp;
415 struct rte_rib_node *tmp = NULL;
416 struct rte_rib_node *node;
417 struct rte_rib_node *parent;
419 uint64_t par_nh, node_nh;
421 if ((fib == NULL) || (depth > RTE_FIB_MAXDEPTH))
424 dp = rte_fib_get_dp(fib);
425 rib = rte_fib_get_rib(fib);
426 RTE_ASSERT((dp != NULL) && (rib != NULL));
428 if (next_hop > get_max_nh(dp->nh_sz))
431 ip &= rte_rib_depth_to_mask(depth);
433 node = rte_rib_lookup_exact(rib, ip, depth);
437 rte_rib_get_nh(node, &node_nh);
438 if (node_nh == next_hop)
440 ret = modify_fib(dp, rib, ip, depth, next_hop);
442 rte_rib_set_nh(node, next_hop);
446 tmp = rte_rib_get_nxt(rib, ip, 24, NULL,
447 RTE_RIB_GET_NXT_COVER);
449 (dp->rsvd_tbl8s >= dp->number_tbl8s))
453 node = rte_rib_insert(rib, ip, depth);
456 rte_rib_set_nh(node, next_hop);
457 parent = rte_rib_lookup_parent(node);
458 if (parent != NULL) {
459 rte_rib_get_nh(parent, &par_nh);
460 if (par_nh == next_hop)
463 ret = modify_fib(dp, rib, ip, depth, next_hop);
465 rte_rib_remove(rib, ip, depth);
468 if ((depth > 24) && (tmp == NULL))
475 parent = rte_rib_lookup_parent(node);
476 if (parent != NULL) {
477 rte_rib_get_nh(parent, &par_nh);
478 rte_rib_get_nh(node, &node_nh);
479 if (par_nh != node_nh)
480 ret = modify_fib(dp, rib, ip, depth, par_nh);
482 ret = modify_fib(dp, rib, ip, depth, dp->def_nh);
484 rte_rib_remove(rib, ip, depth);
486 tmp = rte_rib_get_nxt(rib, ip, 24, NULL,
487 RTE_RIB_GET_NXT_COVER);
500 dir24_8_create(const char *name, int socket_id, struct rte_fib_conf *fib_conf)
502 char mem_name[DIR24_8_NAMESIZE];
503 struct dir24_8_tbl *dp;
506 enum rte_fib_dir24_8_nh_sz nh_sz;
508 if ((name == NULL) || (fib_conf == NULL) ||
509 (fib_conf->dir24_8.nh_sz < RTE_FIB_DIR24_8_1B) ||
510 (fib_conf->dir24_8.nh_sz > RTE_FIB_DIR24_8_8B) ||
511 (fib_conf->dir24_8.num_tbl8 >
512 get_max_nh(fib_conf->dir24_8.nh_sz)) ||
513 (fib_conf->dir24_8.num_tbl8 == 0) ||
514 (fib_conf->default_nh >
515 get_max_nh(fib_conf->dir24_8.nh_sz))) {
520 def_nh = fib_conf->default_nh;
521 nh_sz = fib_conf->dir24_8.nh_sz;
522 num_tbl8 = RTE_ALIGN_CEIL(fib_conf->dir24_8.num_tbl8,
523 BITMAP_SLAB_BIT_SIZE);
525 snprintf(mem_name, sizeof(mem_name), "DP_%s", name);
526 dp = rte_zmalloc_socket(name, sizeof(struct dir24_8_tbl) +
527 DIR24_8_TBL24_NUM_ENT * (1 << nh_sz), RTE_CACHE_LINE_SIZE,
534 /* Init table with default value */
535 write_to_fib(dp->tbl24, (def_nh << 1), nh_sz, 1 << 24);
537 snprintf(mem_name, sizeof(mem_name), "TBL8_%p", dp);
538 uint64_t tbl8_sz = DIR24_8_TBL8_GRP_NUM_ENT * (1ULL << nh_sz) *
540 dp->tbl8 = rte_zmalloc_socket(mem_name, tbl8_sz,
541 RTE_CACHE_LINE_SIZE, socket_id);
542 if (dp->tbl8 == NULL) {
549 dp->number_tbl8s = num_tbl8;
551 snprintf(mem_name, sizeof(mem_name), "TBL8_idxes_%p", dp);
552 dp->tbl8_idxes = rte_zmalloc_socket(mem_name,
553 RTE_ALIGN_CEIL(dp->number_tbl8s, 64) >> 3,
554 RTE_CACHE_LINE_SIZE, socket_id);
555 if (dp->tbl8_idxes == NULL) {
566 dir24_8_free(void *p)
568 struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
570 rte_free(dp->tbl8_idxes);