1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
16 typedef int32_t (*rte_fib_test)(void);
18 static int32_t test_create_invalid(void);
19 static int32_t test_multiple_create(void);
20 static int32_t test_free_null(void);
21 static int32_t test_add_del_invalid(void);
22 static int32_t test_get_invalid(void);
23 static int32_t test_lookup(void);
25 #define MAX_ROUTES (1 << 16)
26 #define MAX_TBL8 (1 << 15)
29 * Check that rte_fib_create fails gracefully for incorrect user input
33 test_create_invalid(void)
35 struct rte_fib *fib = NULL;
36 struct rte_fib_conf config;
38 config.max_routes = MAX_ROUTES;
39 config.default_nh = 0;
40 config.type = RTE_FIB_DUMMY;
42 /* rte_fib_create: fib name == NULL */
43 fib = rte_fib_create(NULL, SOCKET_ID_ANY, &config);
44 RTE_TEST_ASSERT(fib == NULL,
45 "Call succeeded with invalid parameters\n");
47 /* rte_fib_create: config == NULL */
48 fib = rte_fib_create(__func__, SOCKET_ID_ANY, NULL);
49 RTE_TEST_ASSERT(fib == NULL,
50 "Call succeeded with invalid parameters\n");
52 /* socket_id < -1 is invalid */
53 fib = rte_fib_create(__func__, -2, &config);
54 RTE_TEST_ASSERT(fib == NULL,
55 "Call succeeded with invalid parameters\n");
57 /* rte_fib_create: max_routes = 0 */
58 config.max_routes = 0;
59 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
60 RTE_TEST_ASSERT(fib == NULL,
61 "Call succeeded with invalid parameters\n");
62 config.max_routes = MAX_ROUTES;
64 config.type = RTE_FIB_TYPE_MAX;
65 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
66 RTE_TEST_ASSERT(fib == NULL,
67 "Call succeeded with invalid parameters\n");
69 config.type = RTE_FIB_DIR24_8;
70 config.dir24_8.num_tbl8 = MAX_TBL8;
72 config.dir24_8.nh_sz = RTE_FIB_DIR24_8_8B + 1;
73 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
74 RTE_TEST_ASSERT(fib == NULL,
75 "Call succeeded with invalid parameters\n");
76 config.dir24_8.nh_sz = RTE_FIB_DIR24_8_8B;
78 config.dir24_8.num_tbl8 = 0;
79 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
80 RTE_TEST_ASSERT(fib == NULL,
81 "Call succeeded with invalid parameters\n");
87 * Create fib table then delete fib table 10 times
88 * Use a slightly different rules size each time
91 test_multiple_create(void)
93 struct rte_fib *fib = NULL;
94 struct rte_fib_conf config;
97 config.default_nh = 0;
98 config.type = RTE_FIB_DUMMY;
100 for (i = 0; i < 100; i++) {
101 config.max_routes = MAX_ROUTES - i;
102 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
103 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
106 /* Can not test free so return success */
111 * Call rte_fib_free for NULL pointer user input. Note: free has no return and
112 * therefore it is impossible to check for failure but this test is added to
113 * increase function coverage metrics and to validate that freeing null does
119 struct rte_fib *fib = NULL;
120 struct rte_fib_conf config;
122 config.max_routes = MAX_ROUTES;
123 config.default_nh = 0;
124 config.type = RTE_FIB_DUMMY;
126 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
127 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
135 * Check that rte_fib_add and rte_fib_delete fails gracefully
136 * for incorrect user input arguments
139 test_add_del_invalid(void)
141 struct rte_fib *fib = NULL;
142 struct rte_fib_conf config;
144 uint32_t ip = RTE_IPV4(0, 0, 0, 0);
148 config.max_routes = MAX_ROUTES;
149 config.default_nh = 0;
150 config.type = RTE_FIB_DUMMY;
152 /* rte_fib_add: fib == NULL */
153 ret = rte_fib_add(NULL, ip, depth, nh);
154 RTE_TEST_ASSERT(ret < 0,
155 "Call succeeded with invalid parameters\n");
157 /* rte_fib_delete: fib == NULL */
158 ret = rte_fib_delete(NULL, ip, depth);
159 RTE_TEST_ASSERT(ret < 0,
160 "Call succeeded with invalid parameters\n");
162 /*Create valid fib to use in rest of test. */
163 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
164 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
166 /* rte_fib_add: depth > RTE_FIB_MAXDEPTH */
167 ret = rte_fib_add(fib, ip, RTE_FIB_MAXDEPTH + 1, nh);
168 RTE_TEST_ASSERT(ret < 0,
169 "Call succeeded with invalid parameters\n");
171 /* rte_fib_delete: depth > RTE_FIB_MAXDEPTH */
172 ret = rte_fib_delete(fib, ip, RTE_FIB_MAXDEPTH + 1);
173 RTE_TEST_ASSERT(ret < 0,
174 "Call succeeded with invalid parameters\n");
182 * Check that rte_fib_get_dp and rte_fib_get_rib fails gracefully
183 * for incorrect user input arguments
186 test_get_invalid(void)
190 p = rte_fib_get_dp(NULL);
191 RTE_TEST_ASSERT(p == NULL,
192 "Call succeeded with invalid parameters\n");
194 p = rte_fib_get_rib(NULL);
195 RTE_TEST_ASSERT(p == NULL,
196 "Call succeeded with invalid parameters\n");
202 * Add routes for one supernet with all possible depths and do lookup
204 * After delete routes with doing lookup on each step
207 lookup_and_check_asc(struct rte_fib *fib, uint32_t ip_arr[RTE_FIB_MAXDEPTH],
208 uint32_t ip_missing, uint64_t def_nh, uint32_t n)
210 uint64_t nh_arr[RTE_FIB_MAXDEPTH];
214 ret = rte_fib_lookup_bulk(fib, ip_arr, nh_arr, RTE_FIB_MAXDEPTH);
215 RTE_TEST_ASSERT(ret == 0, "Failed to lookup\n");
217 for (; i <= RTE_FIB_MAXDEPTH - n; i++)
218 RTE_TEST_ASSERT(nh_arr[i] == n,
219 "Failed to get proper nexthop\n");
221 for (; i < RTE_FIB_MAXDEPTH; i++)
222 RTE_TEST_ASSERT(nh_arr[i] == --n,
223 "Failed to get proper nexthop\n");
225 ret = rte_fib_lookup_bulk(fib, &ip_missing, nh_arr, 1);
226 RTE_TEST_ASSERT((ret == 0) && (nh_arr[0] == def_nh),
227 "Failed to get proper nexthop\n");
233 lookup_and_check_desc(struct rte_fib *fib, uint32_t ip_arr[RTE_FIB_MAXDEPTH],
234 uint32_t ip_missing, uint64_t def_nh, uint32_t n)
236 uint64_t nh_arr[RTE_FIB_MAXDEPTH];
240 ret = rte_fib_lookup_bulk(fib, ip_arr, nh_arr, RTE_FIB_MAXDEPTH);
241 RTE_TEST_ASSERT(ret == 0, "Failed to lookup\n");
244 RTE_TEST_ASSERT(nh_arr[i] == RTE_FIB_MAXDEPTH - i,
245 "Failed to get proper nexthop\n");
247 for (; i < RTE_FIB_MAXDEPTH; i++)
248 RTE_TEST_ASSERT(nh_arr[i] == def_nh,
249 "Failed to get proper nexthop\n");
251 ret = rte_fib_lookup_bulk(fib, &ip_missing, nh_arr, 1);
252 RTE_TEST_ASSERT((ret == 0) && (nh_arr[0] == def_nh),
253 "Failed to get proper nexthop\n");
259 check_fib(struct rte_fib *fib)
261 uint64_t def_nh = 100;
262 uint32_t ip_arr[RTE_FIB_MAXDEPTH];
263 uint32_t ip_add = RTE_IPV4(128, 0, 0, 0);
264 uint32_t i, ip_missing = RTE_IPV4(127, 255, 255, 255);
267 for (i = 0; i < RTE_FIB_MAXDEPTH; i++)
268 ip_arr[i] = ip_add + (1ULL << i) - 1;
270 ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh, 0);
271 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
273 for (i = 1; i <= RTE_FIB_MAXDEPTH; i++) {
274 ret = rte_fib_add(fib, ip_add, i, i);
275 RTE_TEST_ASSERT(ret == 0, "Failed to add a route\n");
276 ret = lookup_and_check_asc(fib, ip_arr, ip_missing,
278 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
281 for (i = RTE_FIB_MAXDEPTH; i > 1; i--) {
282 ret = rte_fib_delete(fib, ip_add, i);
283 RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
284 ret = lookup_and_check_asc(fib, ip_arr, ip_missing,
287 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
289 ret = rte_fib_delete(fib, ip_add, i);
290 RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
291 ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh, 0);
292 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
294 for (i = 0; i < RTE_FIB_MAXDEPTH; i++) {
295 ret = rte_fib_add(fib, ip_add, RTE_FIB_MAXDEPTH - i,
296 RTE_FIB_MAXDEPTH - i);
297 RTE_TEST_ASSERT(ret == 0, "Failed to add a route\n");
298 ret = lookup_and_check_desc(fib, ip_arr, ip_missing,
300 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
303 for (i = 1; i <= RTE_FIB_MAXDEPTH; i++) {
304 ret = rte_fib_delete(fib, ip_add, i);
305 RTE_TEST_ASSERT(ret == 0, "Failed to delete a route\n");
306 ret = lookup_and_check_desc(fib, ip_arr, ip_missing, def_nh,
307 RTE_FIB_MAXDEPTH - i);
308 RTE_TEST_ASSERT(ret == TEST_SUCCESS, "Lookup and check fails\n");
317 struct rte_fib *fib = NULL;
318 struct rte_fib_conf config;
319 uint64_t def_nh = 100;
322 config.max_routes = MAX_ROUTES;
323 config.default_nh = def_nh;
324 config.type = RTE_FIB_DUMMY;
326 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
327 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
328 ret = check_fib(fib);
329 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
330 "Check_fib fails for DUMMY type\n");
333 config.type = RTE_FIB_DIR24_8;
335 config.dir24_8.nh_sz = RTE_FIB_DIR24_8_1B;
336 config.dir24_8.num_tbl8 = 127;
337 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
338 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
339 ret = check_fib(fib);
340 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
341 "Check_fib fails for DIR24_8_1B type\n");
344 config.dir24_8.nh_sz = RTE_FIB_DIR24_8_2B;
345 config.dir24_8.num_tbl8 = MAX_TBL8 - 1;
346 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
347 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
348 ret = check_fib(fib);
349 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
350 "Check_fib fails for DIR24_8_2B type\n");
353 config.dir24_8.nh_sz = RTE_FIB_DIR24_8_4B;
354 config.dir24_8.num_tbl8 = MAX_TBL8;
355 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
356 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
357 ret = check_fib(fib);
358 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
359 "Check_fib fails for DIR24_8_4B type\n");
362 config.dir24_8.nh_sz = RTE_FIB_DIR24_8_8B;
363 config.dir24_8.num_tbl8 = MAX_TBL8;
364 fib = rte_fib_create(__func__, SOCKET_ID_ANY, &config);
365 RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
366 ret = check_fib(fib);
367 RTE_TEST_ASSERT(ret == TEST_SUCCESS,
368 "Check_fib fails for DIR24_8_8B type\n");
374 static struct unit_test_suite fib_fast_tests = {
375 .suite_name = "fib autotest",
379 TEST_CASE(test_create_invalid),
380 TEST_CASE(test_free_null),
381 TEST_CASE(test_add_del_invalid),
382 TEST_CASE(test_get_invalid),
383 TEST_CASE(test_lookup),
388 static struct unit_test_suite fib_slow_tests = {
389 .suite_name = "fib slow autotest",
393 TEST_CASE(test_multiple_create),
404 return unit_test_suite_runner(&fib_fast_tests);
410 return unit_test_suite_runner(&fib_slow_tests);
413 REGISTER_TEST_COMMAND(fib_autotest, test_fib);
414 REGISTER_TEST_COMMAND(fib_slow_autotest, test_slow_fib);