1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
14 #ifdef RTE_EXEC_ENV_WINDOWS
18 printf("rib6 not supported on Windows, skipping test\n");
25 printf("slow_rib6 not supported on Windows, skipping test\n");
32 typedef int32_t (*rte_rib6_test)(void);
34 static int32_t test_create_invalid(void);
35 static int32_t test_multiple_create(void);
36 static int32_t test_free_null(void);
37 static int32_t test_insert_invalid(void);
38 static int32_t test_get_fn(void);
39 static int32_t test_basic(void);
40 static int32_t test_tree_traversal(void);
43 #define MAX_RULES (1 << 22)
46 * Check that rte_rib6_create fails gracefully for incorrect user input
50 test_create_invalid(void)
52 struct rte_rib6 *rib = NULL;
53 struct rte_rib6_conf config;
55 config.max_nodes = MAX_RULES;
58 /* rte_rib6_create: rib name == NULL */
59 rib = rte_rib6_create(NULL, SOCKET_ID_ANY, &config);
60 RTE_TEST_ASSERT(rib == NULL,
61 "Call succeeded with invalid parameters\n");
63 /* rte_rib6_create: config == NULL */
64 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, NULL);
65 RTE_TEST_ASSERT(rib == NULL,
66 "Call succeeded with invalid parameters\n");
68 /* socket_id < -1 is invalid */
69 rib = rte_rib6_create(__func__, -2, &config);
70 RTE_TEST_ASSERT(rib == NULL,
71 "Call succeeded with invalid parameters\n");
73 /* rte_rib6_create: max_nodes = 0 */
75 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
76 RTE_TEST_ASSERT(rib == NULL,
77 "Call succeeded with invalid parameters\n");
78 config.max_nodes = MAX_RULES;
84 * Create rib table then delete rib table 10 times
85 * Use a slightly different rules size each time
88 test_multiple_create(void)
90 struct rte_rib6 *rib = NULL;
91 struct rte_rib6_conf config;
96 for (i = 0; i < 100; i++) {
97 config.max_nodes = MAX_RULES - i;
98 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
99 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
102 /* Can not test free so return success */
107 * Call rte_rib6_free for NULL pointer user input. Note: free has no return and
108 * therefore it is impossible to check for failure but this test is added to
109 * increase function coverage metrics and to validate that freeing null does
115 struct rte_rib6 *rib = NULL;
116 struct rte_rib6_conf config;
118 config.max_nodes = MAX_RULES;
121 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
122 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
130 * Check that rte_rib6_insert fails gracefully
131 * for incorrect user input arguments
134 test_insert_invalid(void)
136 struct rte_rib6 *rib = NULL;
137 struct rte_rib6_node *node, *node1;
138 struct rte_rib6_conf config;
139 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE] = {0};
142 config.max_nodes = MAX_RULES;
145 /* rte_rib6_insert: rib == NULL */
146 node = rte_rib6_insert(NULL, ip, depth);
147 RTE_TEST_ASSERT(node == NULL,
148 "Call succeeded with invalid parameters\n");
150 /*Create valid rib to use in rest of test. */
151 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
152 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
154 /* rte_rib6_insert: depth > MAX_DEPTH */
155 node = rte_rib6_insert(rib, ip, MAX_DEPTH + 1);
156 RTE_TEST_ASSERT(node == NULL,
157 "Call succeeded with invalid parameters\n");
159 /* insert the same ip/depth twice*/
160 node = rte_rib6_insert(rib, ip, depth);
161 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
162 node1 = rte_rib6_insert(rib, ip, depth);
163 RTE_TEST_ASSERT(node1 == NULL,
164 "Call succeeded with invalid parameters\n");
172 * Call rte_rib6_node access functions with incorrect input.
173 * After call rte_rib6_node access functions with correct args
174 * and check the return values for correctness
179 struct rte_rib6 *rib = NULL;
180 struct rte_rib6_node *node;
181 struct rte_rib6_conf config;
183 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE] = {192, 0, 2, 0, 0, 0, 0, 0,
184 0, 0, 0, 0, 0, 0, 0, 0};
185 uint8_t ip_ret[RTE_RIB6_IPV6_ADDR_SIZE];
186 uint64_t nh_set = 10;
192 config.max_nodes = MAX_RULES;
195 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
196 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
198 node = rte_rib6_insert(rib, ip, depth);
199 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
201 /* test rte_rib6_get_ip() with incorrect args */
202 ret = rte_rib6_get_ip(NULL, ip_ret);
203 RTE_TEST_ASSERT(ret < 0,
204 "Call succeeded with invalid parameters\n");
205 ret = rte_rib6_get_ip(node, NULL);
206 RTE_TEST_ASSERT(ret < 0,
207 "Call succeeded with invalid parameters\n");
209 /* test rte_rib6_get_depth() with incorrect args */
210 ret = rte_rib6_get_depth(NULL, &depth_ret);
211 RTE_TEST_ASSERT(ret < 0,
212 "Call succeeded with invalid parameters\n");
213 ret = rte_rib6_get_depth(node, NULL);
214 RTE_TEST_ASSERT(ret < 0,
215 "Call succeeded with invalid parameters\n");
217 /* test rte_rib6_set_nh() with incorrect args */
218 ret = rte_rib6_set_nh(NULL, nh_set);
219 RTE_TEST_ASSERT(ret < 0,
220 "Call succeeded with invalid parameters\n");
222 /* test rte_rib6_get_nh() with incorrect args */
223 ret = rte_rib6_get_nh(NULL, &nh_ret);
224 RTE_TEST_ASSERT(ret < 0,
225 "Call succeeded with invalid parameters\n");
226 ret = rte_rib6_get_nh(node, NULL);
227 RTE_TEST_ASSERT(ret < 0,
228 "Call succeeded with invalid parameters\n");
230 /* test rte_rib6_get_ext() with incorrect args */
231 ext = rte_rib6_get_ext(NULL);
232 RTE_TEST_ASSERT(ext == NULL,
233 "Call succeeded with invalid parameters\n");
235 /* check the return values */
236 ret = rte_rib6_get_ip(node, ip_ret);
237 RTE_TEST_ASSERT((ret == 0) && (rte_rib6_is_equal(ip_ret, ip)),
238 "Failed to get proper node ip\n");
239 ret = rte_rib6_get_depth(node, &depth_ret);
240 RTE_TEST_ASSERT((ret == 0) && (depth_ret == depth),
241 "Failed to get proper node depth\n");
242 ret = rte_rib6_set_nh(node, nh_set);
243 RTE_TEST_ASSERT(ret == 0,
244 "Failed to set rte_rib_node nexthop\n");
245 ret = rte_rib6_get_nh(node, &nh_ret);
246 RTE_TEST_ASSERT((ret == 0) && (nh_ret == nh_set),
247 "Failed to get proper nexthop\n");
255 * Call insert, lookup/lookup_exact and delete for a single rule
260 struct rte_rib6 *rib = NULL;
261 struct rte_rib6_node *node;
262 struct rte_rib6_conf config;
264 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE] = {192, 0, 2, 0, 0, 0, 0, 0,
265 0, 0, 0, 0, 0, 0, 0, 0};
266 uint64_t next_hop_add = 10;
267 uint64_t next_hop_return;
271 config.max_nodes = MAX_RULES;
274 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
275 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
277 node = rte_rib6_insert(rib, ip, depth);
278 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
280 status = rte_rib6_set_nh(node, next_hop_add);
281 RTE_TEST_ASSERT(status == 0,
282 "Failed to set rte_rib_node field\n");
284 node = rte_rib6_lookup(rib, ip);
285 RTE_TEST_ASSERT(node != NULL, "Failed to lookup\n");
287 status = rte_rib6_get_nh(node, &next_hop_return);
288 RTE_TEST_ASSERT((status == 0) && (next_hop_add == next_hop_return),
289 "Failed to get proper nexthop\n");
291 node = rte_rib6_lookup_exact(rib, ip, depth);
292 RTE_TEST_ASSERT(node != NULL,
293 "Failed to lookup\n");
295 status = rte_rib6_get_nh(node, &next_hop_return);
296 RTE_TEST_ASSERT((status == 0) && (next_hop_add == next_hop_return),
297 "Failed to get proper nexthop\n");
299 rte_rib6_remove(rib, ip, depth);
301 node = rte_rib6_lookup(rib, ip);
302 RTE_TEST_ASSERT(node == NULL,
303 "Lookup returns non existent rule\n");
304 node = rte_rib6_lookup_exact(rib, ip, depth);
305 RTE_TEST_ASSERT(node == NULL,
306 "Lookup returns non existent rule\n");
314 test_tree_traversal(void)
316 struct rte_rib6 *rib = NULL;
317 struct rte_rib6_node *node;
318 struct rte_rib6_conf config;
320 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE] = {10, 0, 2, 130, 0, 0, 0, 0,
321 0, 0, 0, 0, 0, 0, 0, 0};
322 uint8_t ip1[RTE_RIB6_IPV6_ADDR_SIZE] = {10, 0, 2, 0, 0, 0, 0, 0,
323 0, 0, 0, 0, 0, 0, 0, 0};
324 uint8_t ip2[RTE_RIB6_IPV6_ADDR_SIZE] = {10, 0, 2, 130, 0, 0, 0, 0,
325 0, 0, 0, 0, 0, 0, 0, 80};
328 config.max_nodes = MAX_RULES;
331 rib = rte_rib6_create(__func__, SOCKET_ID_ANY, &config);
332 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
334 node = rte_rib6_insert(rib, ip1, depth);
335 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
336 node = rte_rib6_insert(rib, ip2, depth);
337 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
340 node = rte_rib6_get_nxt(rib, ip, 32, node, RTE_RIB6_GET_NXT_ALL);
341 RTE_TEST_ASSERT(node != NULL, "Failed to get rib_node\n");
348 static struct unit_test_suite rib6_tests = {
349 .suite_name = "rib6 autotest",
353 TEST_CASE(test_create_invalid),
354 TEST_CASE(test_free_null),
355 TEST_CASE(test_insert_invalid),
356 TEST_CASE(test_get_fn),
357 TEST_CASE(test_basic),
358 TEST_CASE(test_tree_traversal),
363 static struct unit_test_suite rib6_slow_tests = {
364 .suite_name = "rib6 slow autotest",
368 TEST_CASE(test_multiple_create),
379 return unit_test_suite_runner(&rib6_tests);
385 return unit_test_suite_runner(&rib6_slow_tests);
388 #endif /* !RTE_EXEC_ENV_WINDOWS */
390 REGISTER_TEST_COMMAND(rib6_autotest, test_rib6);
391 REGISTER_TEST_COMMAND(rib6_slow_autotest, test_slow_rib6);