Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 : : * Copyright(c) 2019 Intel Corporation
4 : : */
5 : :
6 : : #include <stdint.h>
7 : : #include <stdio.h>
8 : :
9 : : #include <rte_debug.h>
10 : : #include <rte_malloc.h>
11 : : #include <rte_errno.h>
12 : : #include <rte_vect.h>
13 : : #include <rte_cpuflags.h>
14 : :
15 : : #include <rte_rib.h>
16 : : #include <rte_fib.h>
17 : : #include "dir24_8.h"
18 : : #include "fib_log.h"
19 : :
20 : : #ifdef CC_AVX512_SUPPORT
21 : :
22 : : #include "dir24_8_avx512.h"
23 : :
24 : : #elif defined(RTE_RISCV_FEATURE_V)
25 : :
26 : : #include "dir24_8_rvv.h"
27 : :
28 : : #endif /* CC_AVX512_SUPPORT */
29 : :
30 : : #define DIR24_8_NAMESIZE 64
31 : :
32 : : #define ROUNDUP(x, y) RTE_ALIGN_CEIL(x, (1 << (32 - y)))
33 : :
34 : : static inline rte_fib_lookup_fn_t
35 : : get_scalar_fn(enum rte_fib_dir24_8_nh_sz nh_sz, bool be_addr)
36 : : {
37 [ + + + + : 8 : switch (nh_sz) {
- ]
38 : 1 : case RTE_FIB_DIR24_8_1B:
39 [ - - + - ]: 1 : return be_addr ? dir24_8_lookup_bulk_1b_be : dir24_8_lookup_bulk_1b;
40 : 1 : case RTE_FIB_DIR24_8_2B:
41 [ - - + - ]: 1 : return be_addr ? dir24_8_lookup_bulk_2b_be : dir24_8_lookup_bulk_2b;
42 : 5 : case RTE_FIB_DIR24_8_4B:
43 [ - - + - ]: 5 : return be_addr ? dir24_8_lookup_bulk_4b_be : dir24_8_lookup_bulk_4b;
44 : 1 : case RTE_FIB_DIR24_8_8B:
45 [ - - + - ]: 1 : return be_addr ? dir24_8_lookup_bulk_8b_be : dir24_8_lookup_bulk_8b;
46 : : default:
47 : : return NULL;
48 : : }
49 : : }
50 : :
51 : : static inline rte_fib_lookup_fn_t
52 : : get_scalar_fn_inlined(enum rte_fib_dir24_8_nh_sz nh_sz, bool be_addr)
53 : : {
54 : 0 : switch (nh_sz) {
55 : 0 : case RTE_FIB_DIR24_8_1B:
56 [ # # ]: 0 : return be_addr ? dir24_8_lookup_bulk_0_be : dir24_8_lookup_bulk_0;
57 : 0 : case RTE_FIB_DIR24_8_2B:
58 [ # # ]: 0 : return be_addr ? dir24_8_lookup_bulk_1_be : dir24_8_lookup_bulk_1;
59 : 0 : case RTE_FIB_DIR24_8_4B:
60 [ # # ]: 0 : return be_addr ? dir24_8_lookup_bulk_2_be : dir24_8_lookup_bulk_2;
61 : 0 : case RTE_FIB_DIR24_8_8B:
62 [ # # ]: 0 : return be_addr ? dir24_8_lookup_bulk_3_be : dir24_8_lookup_bulk_3;
63 : : default:
64 : : return NULL;
65 : : }
66 : : }
67 : :
68 : : static inline rte_fib_lookup_fn_t
69 : 8 : get_vector_fn(enum rte_fib_dir24_8_nh_sz nh_sz, bool be_addr)
70 : : {
71 : : #ifdef CC_AVX512_SUPPORT
72 [ + - + - ]: 16 : if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) <= 0 ||
73 [ + - ]: 16 : rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512DQ) <= 0 ||
74 : 8 : rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_512)
75 : 8 : return NULL;
76 : :
77 [ # # # # ]: 0 : if (be_addr && rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512BW) <= 0)
78 : : return NULL;
79 : :
80 [ # # # # : 0 : switch (nh_sz) {
# ]
81 : 0 : case RTE_FIB_DIR24_8_1B:
82 [ # # ]: 0 : return be_addr ? rte_dir24_8_vec_lookup_bulk_1b_be :
83 : : rte_dir24_8_vec_lookup_bulk_1b;
84 : 0 : case RTE_FIB_DIR24_8_2B:
85 [ # # ]: 0 : return be_addr ? rte_dir24_8_vec_lookup_bulk_2b_be :
86 : : rte_dir24_8_vec_lookup_bulk_2b;
87 : 0 : case RTE_FIB_DIR24_8_4B:
88 [ # # ]: 0 : return be_addr ? rte_dir24_8_vec_lookup_bulk_4b_be :
89 : : rte_dir24_8_vec_lookup_bulk_4b;
90 : 0 : case RTE_FIB_DIR24_8_8B:
91 [ # # ]: 0 : return be_addr ? rte_dir24_8_vec_lookup_bulk_8b_be :
92 : : rte_dir24_8_vec_lookup_bulk_8b;
93 : : default:
94 : : return NULL;
95 : : }
96 : : #elif defined(RTE_RISCV_FEATURE_V)
97 : : /* the vector functions take addresses in host order only */
98 : : if (be_addr || rte_cpu_get_flag_enabled(RTE_CPUFLAG_RISCV_ISA_V) <= 0)
99 : : return NULL;
100 : : switch (nh_sz) {
101 : : case RTE_FIB_DIR24_8_1B:
102 : : return rte_dir24_8_vec_lookup_bulk_1b;
103 : : case RTE_FIB_DIR24_8_2B:
104 : : return rte_dir24_8_vec_lookup_bulk_2b;
105 : : case RTE_FIB_DIR24_8_4B:
106 : : return rte_dir24_8_vec_lookup_bulk_4b;
107 : : case RTE_FIB_DIR24_8_8B:
108 : : return rte_dir24_8_vec_lookup_bulk_8b;
109 : : default:
110 : : return NULL;
111 : : }
112 : : #else
113 : : RTE_SET_USED(nh_sz);
114 : : RTE_SET_USED(be_addr);
115 : : #endif
116 : : return NULL;
117 : : }
118 : :
119 : : rte_fib_lookup_fn_t
120 : 8 : dir24_8_get_lookup_fn(void *p, enum rte_fib_lookup_type type, bool be_addr)
121 : : {
122 : : enum rte_fib_dir24_8_nh_sz nh_sz;
123 : : rte_fib_lookup_fn_t ret_fn;
124 : : struct dir24_8_tbl *dp = p;
125 : :
126 [ + - ]: 8 : if (dp == NULL)
127 : : return NULL;
128 : :
129 : 8 : nh_sz = dp->nh_sz;
130 : :
131 [ - - - - : 8 : switch (type) {
+ - ]
132 [ # # # # : 0 : case RTE_FIB_LOOKUP_DIR24_8_SCALAR_MACRO:
# ]
133 : : return get_scalar_fn(nh_sz, be_addr);
134 [ # # # # : 0 : case RTE_FIB_LOOKUP_DIR24_8_SCALAR_INLINE:
# ]
135 : : return get_scalar_fn_inlined(nh_sz, be_addr);
136 : 0 : case RTE_FIB_LOOKUP_DIR24_8_SCALAR_UNI:
137 [ # # ]: 0 : return be_addr ? dir24_8_lookup_bulk_uni_be : dir24_8_lookup_bulk_uni;
138 : 0 : case RTE_FIB_LOOKUP_DIR24_8_VECTOR_AVX512:
139 : 0 : return get_vector_fn(nh_sz, be_addr);
140 : 8 : case RTE_FIB_LOOKUP_DEFAULT:
141 : 8 : ret_fn = get_vector_fn(nh_sz, be_addr);
142 [ + - ]: 8 : return ret_fn != NULL ? ret_fn : get_scalar_fn(nh_sz, be_addr);
143 : : default:
144 : : return NULL;
145 : : }
146 : :
147 : : return NULL;
148 : : }
149 : :
150 : : static void
151 : 2604 : write_to_fib(void *ptr, uint64_t val, enum rte_fib_dir24_8_nh_sz size, int n)
152 : : {
153 : : int i;
154 : : uint8_t *ptr8 = (uint8_t *)ptr;
155 : : uint16_t *ptr16 = (uint16_t *)ptr;
156 : : uint32_t *ptr32 = (uint32_t *)ptr;
157 : : uint64_t *ptr64 = (uint64_t *)ptr;
158 : :
159 [ + + + + : 2604 : switch (size) {
- ]
160 : : case RTE_FIB_DIR24_8_1B:
161 [ + + ]: 67110531 : for (i = 0; i < n; i++)
162 : 67110396 : ptr8[i] = (uint8_t)val;
163 : : break;
164 : : case RTE_FIB_DIR24_8_2B:
165 [ + + ]: 67110531 : for (i = 0; i < n; i++)
166 : 67110396 : ptr16[i] = (uint16_t)val;
167 : : break;
168 : : case RTE_FIB_DIR24_8_4B:
169 [ + + ]: 167990181 : for (i = 0; i < n; i++)
170 : 167987982 : ptr32[i] = (uint32_t)val;
171 : : break;
172 : : case RTE_FIB_DIR24_8_8B:
173 [ + + ]: 67110531 : for (i = 0; i < n; i++)
174 : 67110396 : ptr64[i] = (uint64_t)val;
175 : : break;
176 : : }
177 : 2604 : }
178 : :
179 : : static int
180 : 522 : tbl8_get_idx(struct dir24_8_tbl *dp)
181 : : {
182 : : uint32_t i;
183 : : int bit_idx;
184 : :
185 [ + - ]: 522 : for (i = 0; (i < (dp->number_tbl8s >> BITMAP_SLAB_BIT_SIZE_LOG2)) &&
186 [ - + ]: 522 : (dp->tbl8_idxes[i] == UINT64_MAX); i++)
187 : : ;
188 [ + - ]: 522 : if (i < (dp->number_tbl8s >> BITMAP_SLAB_BIT_SIZE_LOG2)) {
189 : 522 : bit_idx = rte_ctz64(~dp->tbl8_idxes[i]);
190 : 522 : dp->tbl8_idxes[i] |= (1ULL << bit_idx);
191 : 522 : return (i << BITMAP_SLAB_BIT_SIZE_LOG2) + bit_idx;
192 : : }
193 : : return -ENOSPC;
194 : : }
195 : :
196 : : static inline void
197 : : tbl8_free_idx(struct dir24_8_tbl *dp, int idx)
198 : : {
199 : 520 : dp->tbl8_idxes[idx >> BITMAP_SLAB_BIT_SIZE_LOG2] &=
200 : 520 : ~(1ULL << (idx & BITMAP_SLAB_BITMASK));
201 : : }
202 : :
203 : : static int
204 : 522 : tbl8_alloc(struct dir24_8_tbl *dp, uint64_t nh)
205 : : {
206 : : int64_t tbl8_idx;
207 : : uint8_t *tbl8_ptr;
208 : :
209 : 522 : tbl8_idx = tbl8_get_idx(dp);
210 : :
211 : : /* If there are no tbl8 groups try to reclaim one. */
212 [ - + - - : 522 : if (unlikely(tbl8_idx == -ENOSPC && dp->dq &&
- - ]
213 : : !rte_rcu_qsbr_dq_reclaim(dp->dq, 1, NULL, NULL, NULL)))
214 : 0 : tbl8_idx = tbl8_get_idx(dp);
215 : :
216 [ - + ]: 522 : if (tbl8_idx < 0)
217 : 0 : return tbl8_idx;
218 : 522 : tbl8_ptr = (uint8_t *)dp->tbl8 +
219 : 522 : ((tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT) <<
220 : 522 : dp->nh_sz);
221 : : /*Init tbl8 entries with nexthop from tbl24*/
222 : 522 : write_to_fib((void *)tbl8_ptr, nh|
223 : : DIR24_8_EXT_ENT, dp->nh_sz,
224 : : DIR24_8_TBL8_GRP_NUM_ENT);
225 : 522 : dp->cur_tbl8s++;
226 : 522 : return tbl8_idx;
227 : : }
228 : :
229 : : static void
230 : 520 : tbl8_cleanup_and_free(struct dir24_8_tbl *dp, uint64_t tbl8_idx)
231 : : {
232 : 520 : uint8_t *ptr = (uint8_t *)dp->tbl8 + (tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT << dp->nh_sz);
233 : :
234 : 520 : memset(ptr, 0, DIR24_8_TBL8_GRP_NUM_ENT << dp->nh_sz);
235 : 520 : tbl8_free_idx(dp, tbl8_idx);
236 : 520 : dp->cur_tbl8s--;
237 : 520 : }
238 : :
239 : : static void
240 : 0 : __rcu_qsbr_free_resource(void *p, void *data, unsigned int n __rte_unused)
241 : : {
242 : : struct dir24_8_tbl *dp = p;
243 : 0 : uint64_t tbl8_idx = *(uint64_t *)data;
244 : :
245 : 0 : tbl8_cleanup_and_free(dp, tbl8_idx);
246 : 0 : }
247 : :
248 : : static void
249 : 1163 : tbl8_recycle(struct dir24_8_tbl *dp, uint32_t ip, uint64_t tbl8_idx)
250 : : {
251 : : uint32_t i;
252 : : uint64_t nh;
253 : : uint8_t *ptr8;
254 : : uint16_t *ptr16;
255 : : uint32_t *ptr32;
256 : : uint64_t *ptr64;
257 : :
258 [ + + + + : 1163 : switch (dp->nh_sz) {
- ]
259 : 34 : case RTE_FIB_DIR24_8_1B:
260 : 34 : ptr8 = &((uint8_t *)dp->tbl8)[tbl8_idx *
261 : : DIR24_8_TBL8_GRP_NUM_ENT];
262 : 34 : nh = *ptr8;
263 [ + + ]: 1038 : for (i = 1; i < DIR24_8_TBL8_GRP_NUM_ENT; i++) {
264 [ + + ]: 1036 : if (nh != ptr8[i])
265 : : return;
266 : : }
267 : 2 : ((uint8_t *)dp->tbl24)[ip >> 8] =
268 : 2 : nh & ~DIR24_8_EXT_ENT;
269 : 2 : break;
270 : 34 : case RTE_FIB_DIR24_8_2B:
271 : 34 : ptr16 = &((uint16_t *)dp->tbl8)[tbl8_idx *
272 : : DIR24_8_TBL8_GRP_NUM_ENT];
273 : 34 : nh = *ptr16;
274 [ + + ]: 1038 : for (i = 1; i < DIR24_8_TBL8_GRP_NUM_ENT; i++) {
275 [ + + ]: 1036 : if (nh != ptr16[i])
276 : : return;
277 : : }
278 : 2 : ((uint16_t *)dp->tbl24)[ip >> 8] =
279 : 2 : nh & ~DIR24_8_EXT_ENT;
280 : 2 : break;
281 : 1061 : case RTE_FIB_DIR24_8_4B:
282 : 1061 : ptr32 = &((uint32_t *)dp->tbl8)[tbl8_idx *
283 : : DIR24_8_TBL8_GRP_NUM_ENT];
284 : 1061 : nh = *ptr32;
285 [ + + ]: 181614 : for (i = 1; i < DIR24_8_TBL8_GRP_NUM_ENT; i++) {
286 [ + + ]: 181100 : if (nh != ptr32[i])
287 : : return;
288 : : }
289 : 514 : ((uint32_t *)dp->tbl24)[ip >> 8] =
290 : 514 : nh & ~DIR24_8_EXT_ENT;
291 : 514 : break;
292 : 34 : case RTE_FIB_DIR24_8_8B:
293 : 34 : ptr64 = &((uint64_t *)dp->tbl8)[tbl8_idx *
294 : : DIR24_8_TBL8_GRP_NUM_ENT];
295 : 34 : nh = *ptr64;
296 [ + + ]: 1038 : for (i = 1; i < DIR24_8_TBL8_GRP_NUM_ENT; i++) {
297 [ + + ]: 1036 : if (nh != ptr64[i])
298 : : return;
299 : : }
300 : 2 : ((uint64_t *)dp->tbl24)[ip >> 8] =
301 : 2 : nh & ~DIR24_8_EXT_ENT;
302 : 2 : break;
303 : : }
304 : :
305 [ + + ]: 520 : if (dp->v == NULL) {
306 : 8 : tbl8_cleanup_and_free(dp, tbl8_idx);
307 [ + - ]: 512 : } else if (dp->rcu_mode == RTE_FIB_QSBR_MODE_SYNC) {
308 : 512 : rte_rcu_qsbr_synchronize(dp->v, RTE_QSBR_THRID_INVALID);
309 : 512 : tbl8_cleanup_and_free(dp, tbl8_idx);
310 : : } else { /* RTE_FIB_QSBR_MODE_DQ */
311 [ # # ]: 0 : if (rte_rcu_qsbr_dq_enqueue(dp->dq, &tbl8_idx))
312 : 0 : FIB_LOG(ERR, "Failed to push QSBR FIFO");
313 : : }
314 : : }
315 : :
316 : : static int
317 : 1543 : install_to_fib(struct dir24_8_tbl *dp, uint32_t ledge, uint32_t redge,
318 : : uint64_t next_hop)
319 : : {
320 : : uint64_t tbl24_tmp;
321 : : int tbl8_idx;
322 : : int tmp_tbl8_idx;
323 : : uint8_t *tbl8_ptr;
324 : : uint32_t len;
325 : :
326 [ + + ]: 1543 : len = ((ledge == 0) && (redge == 0)) ? 1 << 24 :
327 : 1542 : ((redge & DIR24_8_TBL24_MASK) - ROUNDUP(ledge, 24)) >> 8;
328 : :
329 [ + + + + ]: 1543 : if (((ledge >> 8) != (redge >> 8)) || (len == 1 << 24)) {
330 [ + + ]: 389 : if ((ROUNDUP(ledge, 24) - ledge) != 0) {
331 [ - + ]: 9 : tbl24_tmp = get_tbl24(dp, ledge, dp->nh_sz);
332 [ - + ]: 9 : if ((tbl24_tmp & DIR24_8_EXT_ENT) !=
333 : : DIR24_8_EXT_ENT) {
334 : : /**
335 : : * Make sure there is space for two TBL8.
336 : : * This is necessary when installing range that
337 : : * needs tbl8 for ledge and redge.
338 : : */
339 : 0 : tbl8_idx = tbl8_alloc(dp, tbl24_tmp);
340 : 0 : tmp_tbl8_idx = tbl8_get_idx(dp);
341 [ # # ]: 0 : if (tbl8_idx < 0)
342 : : return -ENOSPC;
343 [ # # ]: 0 : else if (tmp_tbl8_idx < 0) {
344 : : tbl8_free_idx(dp, tbl8_idx);
345 : 0 : return -ENOSPC;
346 : : }
347 : : tbl8_free_idx(dp, tmp_tbl8_idx);
348 : : /*update dir24 entry with tbl8 index*/
349 : 0 : write_to_fib(get_tbl24_p(dp, ledge,
350 : 0 : dp->nh_sz), (tbl8_idx << 1)|
351 : : DIR24_8_EXT_ENT,
352 : : dp->nh_sz, 1);
353 : : } else
354 : 9 : tbl8_idx = tbl24_tmp >> 1;
355 : 9 : tbl8_ptr = (uint8_t *)dp->tbl8 +
356 : 9 : (((tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT) +
357 : 9 : (ledge & ~DIR24_8_TBL24_MASK)) <<
358 : 9 : dp->nh_sz);
359 : : /*update tbl8 with new next hop*/
360 : 9 : write_to_fib((void *)tbl8_ptr, (next_hop << 1)|
361 : : DIR24_8_EXT_ENT,
362 : 9 : dp->nh_sz, ROUNDUP(ledge, 24) - ledge);
363 : 9 : tbl8_recycle(dp, ledge, tbl8_idx);
364 : : }
365 : 389 : write_to_fib(get_tbl24_p(dp, ROUNDUP(ledge, 24), dp->nh_sz),
366 : : next_hop << 1, dp->nh_sz, len);
367 [ - + ]: 389 : if (redge & ~DIR24_8_TBL24_MASK) {
368 [ # # ]: 0 : tbl24_tmp = get_tbl24(dp, redge, dp->nh_sz);
369 [ # # ]: 0 : if ((tbl24_tmp & DIR24_8_EXT_ENT) !=
370 : : DIR24_8_EXT_ENT) {
371 : 0 : tbl8_idx = tbl8_alloc(dp, tbl24_tmp);
372 [ # # ]: 0 : if (tbl8_idx < 0)
373 : : return -ENOSPC;
374 : : /*update dir24 entry with tbl8 index*/
375 : 0 : write_to_fib(get_tbl24_p(dp, redge,
376 : 0 : dp->nh_sz), (tbl8_idx << 1)|
377 : : DIR24_8_EXT_ENT,
378 : : dp->nh_sz, 1);
379 : : } else
380 : 0 : tbl8_idx = tbl24_tmp >> 1;
381 : 0 : tbl8_ptr = (uint8_t *)dp->tbl8 +
382 : 0 : ((tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT) <<
383 : 0 : dp->nh_sz);
384 : : /*update tbl8 with new next hop*/
385 : 0 : write_to_fib((void *)tbl8_ptr, (next_hop << 1)|
386 : : DIR24_8_EXT_ENT,
387 : 0 : dp->nh_sz, redge & ~DIR24_8_TBL24_MASK);
388 : 0 : tbl8_recycle(dp, redge, tbl8_idx);
389 : : }
390 [ + - ]: 1154 : } else if ((redge - ledge) != 0) {
391 [ + + ]: 1154 : tbl24_tmp = get_tbl24(dp, ledge, dp->nh_sz);
392 [ + + ]: 1154 : if ((tbl24_tmp & DIR24_8_EXT_ENT) !=
393 : : DIR24_8_EXT_ENT) {
394 : 522 : tbl8_idx = tbl8_alloc(dp, tbl24_tmp);
395 [ + - ]: 522 : if (tbl8_idx < 0)
396 : : return -ENOSPC;
397 : : /*update dir24 entry with tbl8 index*/
398 : 522 : write_to_fib(get_tbl24_p(dp, ledge, dp->nh_sz),
399 : 522 : (tbl8_idx << 1)|
400 : : DIR24_8_EXT_ENT,
401 : : dp->nh_sz, 1);
402 : : } else
403 : 632 : tbl8_idx = tbl24_tmp >> 1;
404 : 1154 : tbl8_ptr = (uint8_t *)dp->tbl8 +
405 : 1154 : (((tbl8_idx * DIR24_8_TBL8_GRP_NUM_ENT) +
406 : 1154 : (ledge & ~DIR24_8_TBL24_MASK)) <<
407 : 1154 : dp->nh_sz);
408 : : /*update tbl8 with new next hop*/
409 : 1154 : write_to_fib((void *)tbl8_ptr, (next_hop << 1)|
410 : : DIR24_8_EXT_ENT,
411 : 1154 : dp->nh_sz, redge - ledge);
412 : 1154 : tbl8_recycle(dp, ledge, tbl8_idx);
413 : : }
414 : : return 0;
415 : : }
416 : :
417 : : static int
418 : 1545 : modify_fib(struct dir24_8_tbl *dp, struct rte_rib *rib, uint32_t ip,
419 : : uint8_t depth, uint64_t next_hop)
420 : : {
421 : : struct rte_rib_node *tmp = NULL;
422 : : uint32_t ledge, redge, tmp_ip;
423 : : int ret;
424 : : uint8_t tmp_depth;
425 : :
426 : : ledge = ip;
427 : : do {
428 : 1795 : tmp = rte_rib_get_nxt(rib, ip, depth, tmp,
429 : : RTE_RIB_GET_NXT_COVER);
430 [ + + ]: 1795 : if (tmp != NULL) {
431 : 252 : rte_rib_get_depth(tmp, &tmp_depth);
432 [ - + ]: 252 : if (tmp_depth == depth)
433 : 0 : continue;
434 : 252 : rte_rib_get_ip(tmp, &tmp_ip);
435 [ + - ]: 252 : redge = tmp_ip & rte_rib_depth_to_mask(tmp_depth);
436 [ + - ]: 252 : if (ledge == redge) {
437 : 252 : ledge = redge +
438 : 252 : (uint32_t)(1ULL << (32 - tmp_depth));
439 : : /*
440 : : * we got to the end of address space
441 : : * and wrapped around
442 : : */
443 [ + + ]: 252 : if (ledge == 0)
444 : : break;
445 : 250 : continue;
446 : : }
447 : 0 : ret = install_to_fib(dp, ledge, redge,
448 : : next_hop);
449 [ # # ]: 0 : if (ret != 0)
450 : 0 : return ret;
451 : 0 : ledge = redge +
452 : 0 : (uint32_t)(1ULL << (32 - tmp_depth));
453 : : /*
454 : : * we got to the end of address space
455 : : * and wrapped around
456 : : */
457 [ # # ]: 0 : if (ledge == 0)
458 : : break;
459 : : } else {
460 : 1543 : redge = ip + (uint32_t)(1ULL << (32 - depth));
461 [ + - ]: 1543 : if (ledge == redge && ledge != 0)
462 : : break;
463 : 1543 : ret = install_to_fib(dp, ledge, redge,
464 : : next_hop);
465 [ - + ]: 1543 : if (ret != 0)
466 : 0 : return ret;
467 : : }
468 [ + + ]: 1793 : } while (tmp);
469 : :
470 : : return 0;
471 : : }
472 : :
473 : : int
474 : 1545 : dir24_8_modify(struct rte_fib *fib, uint32_t ip, uint8_t depth,
475 : : uint64_t next_hop, int op)
476 : : {
477 : : struct dir24_8_tbl *dp;
478 : : struct rte_rib *rib;
479 : : struct rte_rib_node *tmp = NULL;
480 : : struct rte_rib_node *node;
481 : : struct rte_rib_node *parent;
482 : : int ret = 0;
483 : : uint64_t par_nh, node_nh;
484 : :
485 [ + - ]: 1545 : if ((fib == NULL) || (depth > RTE_FIB_MAXDEPTH))
486 : : return -EINVAL;
487 : :
488 : 1545 : dp = rte_fib_get_dp(fib);
489 : 1545 : rib = rte_fib_get_rib(fib);
490 : : RTE_ASSERT((dp != NULL) && (rib != NULL));
491 : :
492 [ + - ]: 1545 : if (next_hop > get_max_nh(dp->nh_sz))
493 : : return -EINVAL;
494 : :
495 : 1545 : ip &= rte_rib_depth_to_mask(depth);
496 : :
497 : 1545 : node = rte_rib_lookup_exact(rib, ip, depth);
498 [ + + - ]: 1545 : switch (op) {
499 : 776 : case RTE_FIB_ADD:
500 [ - + ]: 776 : if (node != NULL) {
501 : 0 : rte_rib_get_nh(node, &node_nh);
502 [ # # ]: 0 : if (node_nh == next_hop)
503 : : return 0;
504 : 0 : ret = modify_fib(dp, rib, ip, depth, next_hop);
505 [ # # ]: 0 : if (ret == 0)
506 : 0 : rte_rib_set_nh(node, next_hop);
507 : 0 : return 0;
508 : : }
509 [ + + ]: 776 : if (depth > 24) {
510 : 579 : tmp = rte_rib_get_nxt(rib, ip, 24, NULL,
511 : : RTE_RIB_GET_NXT_COVER);
512 [ + + ]: 579 : if ((tmp == NULL) &&
513 [ + - ]: 522 : (dp->rsvd_tbl8s >= dp->number_tbl8s))
514 : : return -ENOSPC;
515 : :
516 : : }
517 : 776 : node = rte_rib_insert(rib, ip, depth);
518 [ - + ]: 776 : if (node == NULL)
519 : 0 : return -rte_errno;
520 : 776 : rte_rib_set_nh(node, next_hop);
521 : 776 : parent = rte_rib_lookup_parent(node);
522 [ + + ]: 776 : if (parent != NULL) {
523 : 126 : rte_rib_get_nh(parent, &par_nh);
524 [ - + ]: 126 : if (par_nh == next_hop)
525 : 0 : goto successfully_added;
526 : : }
527 : 776 : ret = modify_fib(dp, rib, ip, depth, next_hop);
528 [ - + ]: 776 : if (ret != 0) {
529 : 0 : rte_rib_remove(rib, ip, depth);
530 : 0 : return ret;
531 : : }
532 : 776 : successfully_added:
533 [ + + ]: 776 : if ((depth > 24) && (tmp == NULL))
534 : 522 : dp->rsvd_tbl8s++;
535 : : return 0;
536 : 769 : case RTE_FIB_DEL:
537 [ + - ]: 769 : if (node == NULL)
538 : : return -ENOENT;
539 : :
540 : 769 : parent = rte_rib_lookup_parent(node);
541 [ + + ]: 769 : if (parent != NULL) {
542 : 124 : rte_rib_get_nh(parent, &par_nh);
543 : 124 : rte_rib_get_nh(node, &node_nh);
544 [ + - ]: 124 : if (par_nh != node_nh)
545 : 124 : ret = modify_fib(dp, rib, ip, depth, par_nh);
546 : : } else
547 : 645 : ret = modify_fib(dp, rib, ip, depth, dp->def_nh);
548 [ + - ]: 769 : if (ret == 0) {
549 : 769 : rte_rib_remove(rib, ip, depth);
550 [ + + ]: 769 : if (depth > 24) {
551 : 576 : tmp = rte_rib_get_nxt(rib, ip, 24, NULL,
552 : : RTE_RIB_GET_NXT_COVER);
553 [ + + ]: 576 : if (tmp == NULL)
554 : 520 : dp->rsvd_tbl8s--;
555 : : }
556 : : }
557 : : return ret;
558 : : default:
559 : : break;
560 : : }
561 : : return -EINVAL;
562 : : }
563 : :
564 : : void *
565 : 10 : dir24_8_create(const char *name, int socket_id, struct rte_fib_conf *fib_conf)
566 : : {
567 : : char mem_name[DIR24_8_NAMESIZE];
568 : : struct dir24_8_tbl *dp;
569 : : uint64_t def_nh;
570 : : uint32_t num_tbl8;
571 : : enum rte_fib_dir24_8_nh_sz nh_sz;
572 : :
573 [ + - ]: 10 : if ((name == NULL) || (fib_conf == NULL) ||
574 : 10 : (fib_conf->dir24_8.nh_sz < RTE_FIB_DIR24_8_1B) ||
575 [ + + + - ]: 10 : (fib_conf->dir24_8.nh_sz > RTE_FIB_DIR24_8_8B) ||
576 : 9 : (fib_conf->dir24_8.num_tbl8 >
577 [ + - + + ]: 9 : get_max_nh(fib_conf->dir24_8.nh_sz)) ||
578 : 8 : (fib_conf->dir24_8.num_tbl8 == 0) ||
579 [ - + ]: 8 : (fib_conf->default_nh >
580 : : get_max_nh(fib_conf->dir24_8.nh_sz))) {
581 : 2 : rte_errno = EINVAL;
582 : 2 : return NULL;
583 : : }
584 : :
585 : : def_nh = fib_conf->default_nh;
586 : : nh_sz = fib_conf->dir24_8.nh_sz;
587 : 8 : num_tbl8 = RTE_ALIGN_CEIL(fib_conf->dir24_8.num_tbl8,
588 : : BITMAP_SLAB_BIT_SIZE);
589 : :
590 : : snprintf(mem_name, sizeof(mem_name), "DP_%s", name);
591 : 8 : dp = rte_zmalloc_socket(name, sizeof(struct dir24_8_tbl) +
592 : 8 : DIR24_8_TBL24_NUM_ENT * (1 << nh_sz) + sizeof(uint32_t),
593 : : RTE_CACHE_LINE_SIZE, socket_id);
594 [ - + ]: 8 : if (dp == NULL) {
595 : 0 : rte_errno = ENOMEM;
596 : 0 : return NULL;
597 : : }
598 : :
599 : : /* Init table with default value */
600 : 8 : write_to_fib(dp->tbl24, (def_nh << 1), nh_sz, 1 << 24);
601 : :
602 : : snprintf(mem_name, sizeof(mem_name), "TBL8_%p", dp);
603 : 8 : uint64_t tbl8_sz = DIR24_8_TBL8_GRP_NUM_ENT * (1ULL << nh_sz) *
604 : 8 : (num_tbl8 + 1);
605 : 8 : dp->tbl8 = rte_zmalloc_socket(mem_name, tbl8_sz,
606 : : RTE_CACHE_LINE_SIZE, socket_id);
607 [ - + ]: 8 : if (dp->tbl8 == NULL) {
608 : 0 : rte_errno = ENOMEM;
609 : 0 : rte_free(dp);
610 : 0 : return NULL;
611 : : }
612 : 8 : dp->def_nh = def_nh;
613 : 8 : dp->nh_sz = nh_sz;
614 : 8 : dp->number_tbl8s = num_tbl8;
615 : :
616 : : snprintf(mem_name, sizeof(mem_name), "TBL8_idxes_%p", dp);
617 : 16 : dp->tbl8_idxes = rte_zmalloc_socket(mem_name,
618 : 8 : RTE_ALIGN_CEIL(dp->number_tbl8s, 64) >> 3,
619 : : RTE_CACHE_LINE_SIZE, socket_id);
620 [ - + ]: 8 : if (dp->tbl8_idxes == NULL) {
621 : 0 : rte_errno = ENOMEM;
622 : 0 : rte_free(dp->tbl8);
623 : 0 : rte_free(dp);
624 : 0 : return NULL;
625 : : }
626 : :
627 : : return dp;
628 : : }
629 : :
630 : : void
631 : 8 : dir24_8_free(void *p)
632 : : {
633 : : struct dir24_8_tbl *dp = (struct dir24_8_tbl *)p;
634 : :
635 : 8 : rte_rcu_qsbr_dq_delete(dp->dq);
636 : 8 : rte_free(dp->tbl8_idxes);
637 : 8 : rte_free(dp->tbl8);
638 : 8 : rte_free(dp);
639 : 8 : }
640 : :
641 : : int
642 : 5 : dir24_8_rcu_qsbr_add(struct dir24_8_tbl *dp, struct rte_fib_rcu_config *cfg,
643 : : const char *name)
644 : : {
645 : 5 : struct rte_rcu_qsbr_dq_parameters params = {0};
646 : : char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
647 : :
648 [ + + ]: 5 : if (dp == NULL || cfg == NULL)
649 : : return -EINVAL;
650 : :
651 [ + + ]: 4 : if (dp->v != NULL)
652 : : return -EEXIST;
653 : :
654 [ + + ]: 3 : if (cfg->mode == RTE_FIB_QSBR_MODE_SYNC) {
655 : : /* No other things to do. */
656 [ + + ]: 2 : } else if (cfg->mode == RTE_FIB_QSBR_MODE_DQ) {
657 : : /* Init QSBR defer queue. */
658 : : snprintf(rcu_dq_name, sizeof(rcu_dq_name),
659 : : "FIB_RCU_%s", name);
660 : 1 : params.name = rcu_dq_name;
661 : 1 : params.size = cfg->dq_size;
662 [ + - ]: 1 : if (params.size == 0)
663 : 1 : params.size = RTE_FIB_RCU_DQ_RECLAIM_SZ;
664 : 1 : params.trigger_reclaim_limit = cfg->reclaim_thd;
665 : 1 : params.max_reclaim_size = cfg->reclaim_max;
666 [ + - ]: 1 : if (params.max_reclaim_size == 0)
667 : 1 : params.max_reclaim_size = RTE_FIB_RCU_DQ_RECLAIM_MAX;
668 : 1 : params.esize = sizeof(uint64_t);
669 : 1 : params.free_fn = __rcu_qsbr_free_resource;
670 : 1 : params.p = dp;
671 : 1 : params.v = cfg->v;
672 : 1 : dp->dq = rte_rcu_qsbr_dq_create(¶ms);
673 [ - + ]: 1 : if (dp->dq == NULL) {
674 : 0 : FIB_LOG(ERR, "LPM defer queue creation failed");
675 : 0 : return -rte_errno;
676 : : }
677 : : } else {
678 : : return -EINVAL;
679 : : }
680 : :
681 : 2 : dp->rcu_mode = cfg->mode;
682 : 2 : dp->v = cfg->v;
683 : :
684 : 2 : return 0;
685 : : }
|