1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
10 #include <rte_memory.h>
15 #ifdef RTE_EXEC_ENV_WINDOWS
19 printf("fib not supported on Windows, skipping test\n");
26 printf("slow_fib not supported on Windows, skipping test\n");
35 typedef int32_t (*rte_fib6_test)(void);
37 static int32_t test_create_invalid(void);
38 static int32_t test_multiple_create(void);
39 static int32_t test_free_null(void);
40 static int32_t test_add_del_invalid(void);
41 static int32_t test_get_invalid(void);
42 static int32_t test_lookup(void);
44 #define MAX_ROUTES (1 << 16)
45 /** Maximum number of tbl8 for 2-byte entries */
46 #define MAX_TBL8 (1 << 15)
49 * Check that rte_fib6_create fails gracefully for incorrect user input
53 test_create_invalid(void)
55 struct rte_fib6 *fib = NULL;
56 struct rte_fib6_conf config;
58 config.max_routes = MAX_ROUTES;
59 config.rib_ext_sz = 0;
60 config.default_nh = 0;
61 config.type = RTE_FIB6_DUMMY;
63 /* rte_fib6_create: fib name == NULL */
64 fib = rte_fib6_create(NULL, SOCKET_ID_ANY, &config);
65 RTE_TEST_ASSERT(fib == NULL,
66 "Call succeeded with invalid parameters\n");
68 /* rte_fib6_create: config == NULL */
69 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, NULL);
70 RTE_TEST_ASSERT(fib == NULL,
71 "Call succeeded with invalid parameters\n");
73 /* socket_id < -1 is invalid */
74 fib = rte_fib6_create(__func__, -2, &config);
75 RTE_TEST_ASSERT(fib == NULL,
76 "Call succeeded with invalid parameters\n");
78 /* rte_fib6_create: max_routes = 0 */
79 config.max_routes = 0;
80 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
81 RTE_TEST_ASSERT(fib == NULL,
82 "Call succeeded with invalid parameters\n");
83 config.max_routes = MAX_ROUTES;
85 config.type = RTE_FIB6_TRIE + 1;
86 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
87 RTE_TEST_ASSERT(fib == NULL,
88 "Call succeeded with invalid parameters\n");
90 config.type = RTE_FIB6_TRIE;
91 config.trie.num_tbl8 = MAX_TBL8;
93 config.trie.nh_sz = RTE_FIB6_TRIE_8B + 1;
94 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
95 RTE_TEST_ASSERT(fib == NULL,
96 "Call succeeded with invalid parameters\n");
97 config.trie.nh_sz = RTE_FIB6_TRIE_8B;
99 config.trie.num_tbl8 = 0;
100 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
101 RTE_TEST_ASSERT(fib == NULL,
102 "Call succeeded with invalid parameters\n");
108 * Create fib table then delete fib table 10 times
109 * Use a slightly different rules size each time
112 test_multiple_create(void)
114 struct rte_fib6 *fib = NULL;
115 struct rte_fib6_conf config;
118 config.rib_ext_sz = 0;
119 config.default_nh = 0;
120 config.type = RTE_FIB6_DUMMY;
122 for (i = 0; i < 100; i++) {
123 config.max_routes = MAX_ROUTES - i;
124 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
125 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
128 /* Can not test free so return success */
133 * Call rte_fib6_free for NULL pointer user input. Note: free has no return and
134 * therefore it is impossible to check for failure but this test is added to
135 * increase function coverage metrics and to validate that freeing null does
141 struct rte_fib6 *fib = NULL;
142 struct rte_fib6_conf config;
144 config.max_routes = MAX_ROUTES;
145 config.rib_ext_sz = 0;
146 config.default_nh = 0;
147 config.type = RTE_FIB6_DUMMY;
149 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
150 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
159 * Check that rte_fib6_add and rte_fib6_delete fails gracefully
160 * for incorrect user input arguments
163 test_add_del_invalid(void)
165 struct rte_fib6 *fib = NULL;
166 struct rte_fib6_conf config;
168 uint8_t ip[RTE_FIB6_IPV6_ADDR_SIZE] = {0};
172 config.max_routes = MAX_ROUTES;
173 config.rib_ext_sz = 0;
174 config.default_nh = 0;
175 config.type = RTE_FIB6_DUMMY;
177 /* rte_fib6_add: fib == NULL */
178 ret = rte_fib6_add(NULL, ip, depth, nh);
179 RTE_TEST_ASSERT(ret < 0,
180 "Call succeeded with invalid parameters\n");
182 /* rte_fib6_delete: fib == NULL */
183 ret = rte_fib6_delete(NULL, ip, depth);
184 RTE_TEST_ASSERT(ret < 0,
185 "Call succeeded with invalid parameters\n");
187 /*Create valid fib to use in rest of test. */
188 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
189 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
191 /* rte_fib6_add: depth > RTE_FIB6_MAXDEPTH */
192 ret = rte_fib6_add(fib, ip, RTE_FIB6_MAXDEPTH + 1, nh);
193 RTE_TEST_ASSERT(ret < 0,
194 "Call succeeded with invalid parameters\n");
196 /* rte_fib6_delete: depth > RTE_FIB6_MAXDEPTH */
197 ret = rte_fib6_delete(fib, ip, RTE_FIB6_MAXDEPTH + 1);
198 RTE_TEST_ASSERT(ret < 0,
199 "Call succeeded with invalid parameters\n");
207 * Check that rte_fib6_get_dp and rte_fib6_get_rib fails gracefully
208 * for incorrect user input arguments
211 test_get_invalid(void)
215 p = rte_fib6_get_dp(NULL);
216 RTE_TEST_ASSERT(p == NULL,
217 "Call succeeded with invalid parameters\n");
219 p = rte_fib6_get_rib(NULL);
220 RTE_TEST_ASSERT(p == NULL,
221 "Call succeeded with invalid parameters\n");
227 * Add routes for one supernet with all possible depths and do lookup
229 * After delete routes with doing lookup on each step
232 lookup_and_check_asc(struct rte_fib6 *fib,
233 uint8_t ip_arr[RTE_FIB6_MAXDEPTH][RTE_FIB6_IPV6_ADDR_SIZE],
234 uint8_t ip_missing[][RTE_FIB6_IPV6_ADDR_SIZE], uint64_t def_nh,
237 uint64_t nh_arr[RTE_FIB6_MAXDEPTH];
241 ret = rte_fib6_lookup_bulk(fib, ip_arr, nh_arr, RTE_FIB6_MAXDEPTH);
242 RTE_TEST_ASSERT(ret == 0, "Failed to lookup\n");
244 for (; i <= RTE_FIB6_MAXDEPTH - n; i++)
245 RTE_TEST_ASSERT(nh_arr[i] == n,
246 "Failed to get proper nexthop\n");
248 for (; i < RTE_FIB6_MAXDEPTH; i++)
249 RTE_TEST_ASSERT(nh_arr[i] == --n,
250 "Failed to get proper nexthop\n");
252 ret = rte_fib6_lookup_bulk(fib, ip_missing, nh_arr, 1);
253 RTE_TEST_ASSERT((ret == 0) && (nh_arr[0] == def_nh),
254 "Failed to get proper nexthop\n");
260 lookup_and_check_desc(struct rte_fib6 *fib,
261 uint8_t ip_arr[RTE_FIB6_MAXDEPTH][RTE_FIB6_IPV6_ADDR_SIZE],
262 uint8_t ip_missing[][RTE_FIB6_IPV6_ADDR_SIZE], uint64_t def_nh,
265 uint64_t nh_arr[RTE_FIB6_MAXDEPTH];
269 ret = rte_fib6_lookup_bulk(fib, ip_arr, nh_arr, RTE_FIB6_MAXDEPTH);
270 RTE_TEST_ASSERT(ret == 0, "Failed to lookup\n");
273 RTE_TEST_ASSERT(nh_arr[i] == RTE_FIB6_MAXDEPTH - i,
274 "Failed to get proper nexthop\n");
276 for (; i < RTE_FIB6_MAXDEPTH; i++)
277 RTE_TEST_ASSERT(nh_arr[i] == def_nh,
278 "Failed to get proper nexthop\n");
280 ret = rte_fib6_lookup_bulk(fib, ip_missing, nh_arr, 1);
281 RTE_TEST_ASSERT((ret == 0) && (nh_arr[0] == def_nh),
282 "Failed to get proper nexthop\n");
288 check_fib(struct rte_fib6 *fib)
290 uint64_t def_nh = 100;
291 uint8_t ip_arr[RTE_FIB6_MAXDEPTH][RTE_FIB6_IPV6_ADDR_SIZE];
292 uint8_t ip_add[RTE_FIB6_IPV6_ADDR_SIZE] = {0};
293 uint8_t ip_missing[1][RTE_FIB6_IPV6_ADDR_SIZE] = { {255} };
298 ip_missing[0][0] = 127;
299 for (i = 0; i < RTE_FIB6_MAXDEPTH; i++) {
300 for (j = 0; j < RTE_FIB6_IPV6_ADDR_SIZE; j++) {
301 ip_arr[i][j] = ip_add[j] |
302 ~get_msk_part(RTE_FIB6_MAXDEPTH - i, j);
306 ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh, 0);
307 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
309 for (i = 1; i <= RTE_FIB6_MAXDEPTH; i++) {
310 ret = rte_fib6_add(fib, ip_add, i, i);
311 RTE_TEST_ASSERT(ret == 0, "Failed to add a route\n");
312 ret = lookup_and_check_asc(fib, ip_arr, ip_missing, def_nh, i);
313 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
314 "Lookup and check fails\n");
317 for (i = RTE_FIB6_MAXDEPTH; i > 1; i--) {
318 ret = rte_fib6_delete(fib, ip_add, i);
319 RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
320 ret = lookup_and_check_asc(fib, ip_arr, ip_missing,
323 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
324 "Lookup and check fails\n");
326 ret = rte_fib6_delete(fib, ip_add, i);
327 RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
328 ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh, 0);
329 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
330 "Lookup and check fails\n");
332 for (i = 0; i < RTE_FIB6_MAXDEPTH; i++) {
333 ret = rte_fib6_add(fib, ip_add, RTE_FIB6_MAXDEPTH - i,
334 RTE_FIB6_MAXDEPTH - i);
335 RTE_TEST_ASSERT(ret == 0, "Failed to add a route\n");
336 ret = lookup_and_check_desc(fib, ip_arr, ip_missing,
338 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
339 "Lookup and check fails\n");
342 for (i = 1; i <= RTE_FIB6_MAXDEPTH; i++) {
343 ret = rte_fib6_delete(fib, ip_add, i);
344 RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
345 ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh,
346 RTE_FIB6_MAXDEPTH - i);
347 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
348 "Lookup and check fails\n");
357 struct rte_fib6 *fib = NULL;
358 struct rte_fib6_conf config;
359 uint64_t def_nh = 100;
362 config.max_routes = MAX_ROUTES;
363 config.rib_ext_sz = 0;
364 config.default_nh = def_nh;
365 config.type = RTE_FIB6_DUMMY;
367 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
368 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
369 ret = check_fib(fib);
370 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
371 "Check_fib fails for DUMMY type\n");
374 config.type = RTE_FIB6_TRIE;
376 config.trie.nh_sz = RTE_FIB6_TRIE_2B;
377 config.trie.num_tbl8 = MAX_TBL8 - 1;
378 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
379 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
380 ret = check_fib(fib);
381 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
382 "Check_fib fails for TRIE_2B type\n");
385 config.trie.nh_sz = RTE_FIB6_TRIE_4B;
386 config.trie.num_tbl8 = MAX_TBL8;
387 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
388 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
389 ret = check_fib(fib);
390 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
391 "Check_fib fails for TRIE_4B type\n");
394 config.trie.nh_sz = RTE_FIB6_TRIE_8B;
395 config.trie.num_tbl8 = MAX_TBL8;
396 fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
397 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
398 ret = check_fib(fib);
399 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
400 "Check_fib fails for TRIE_8B type\n");
406 static struct unit_test_suite fib6_fast_tests = {
407 .suite_name = "fib6 autotest",
411 TEST_CASE(test_create_invalid),
412 TEST_CASE(test_free_null),
413 TEST_CASE(test_add_del_invalid),
414 TEST_CASE(test_get_invalid),
415 TEST_CASE(test_lookup),
420 static struct unit_test_suite fib6_slow_tests = {
421 .suite_name = "fib6 slow autotest",
425 TEST_CASE(test_multiple_create),
436 return unit_test_suite_runner(&fib6_fast_tests);
442 return unit_test_suite_runner(&fib6_slow_tests);
445 #endif /* !RTE_EXEC_ENV_WINDOWS */
447 REGISTER_TEST_COMMAND(fib6_autotest, test_fib6);
448 REGISTER_TEST_COMMAND(fib6_slow_autotest, test_slow_fib6);