1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
15 typedef int32_t (*rte_rib_test)(void);
17 static int32_t test_create_invalid(void);
18 static int32_t test_multiple_create(void);
19 static int32_t test_free_null(void);
20 static int32_t test_insert_invalid(void);
21 static int32_t test_get_fn(void);
22 static int32_t test_basic(void);
23 static int32_t test_tree_traversal(void);
26 #define MAX_RULES (1 << 22)
29 * Check that rte_rib_create fails gracefully for incorrect user input
33 test_create_invalid(void)
35 struct rte_rib *rib = NULL;
36 struct rte_rib_conf config;
38 config.max_nodes = MAX_RULES;
41 /* rte_rib_create: rib name == NULL */
42 rib = rte_rib_create(NULL, SOCKET_ID_ANY, &config);
43 RTE_TEST_ASSERT(rib == NULL,
44 "Call succeeded with invalid parameters\n");
46 /* rte_rib_create: config == NULL */
47 rib = rte_rib_create(__func__, SOCKET_ID_ANY, NULL);
48 RTE_TEST_ASSERT(rib == NULL,
49 "Call succeeded with invalid parameters\n");
51 /* socket_id < -1 is invalid */
52 rib = rte_rib_create(__func__, -2, &config);
53 RTE_TEST_ASSERT(rib == NULL,
54 "Call succeeded with invalid parameters\n");
56 /* rte_rib_create: max_nodes = 0 */
58 rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
59 RTE_TEST_ASSERT(rib == NULL,
60 "Call succeeded with invalid parameters\n");
61 config.max_nodes = MAX_RULES;
67 * Create rib table then delete rib table 10 times
68 * Use a slightly different rules size each time
71 test_multiple_create(void)
73 struct rte_rib *rib = NULL;
74 struct rte_rib_conf config;
79 for (i = 0; i < 100; i++) {
80 config.max_nodes = MAX_RULES - i;
81 rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
82 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
85 /* Can not test free so return success */
90 * Call rte_rib_free for NULL pointer user input. Note: free has no return and
91 * therefore it is impossible to check for failure but this test is added to
92 * increase function coverage metrics and to validate that freeing null does
98 struct rte_rib *rib = NULL;
99 struct rte_rib_conf config;
101 config.max_nodes = MAX_RULES;
104 rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
105 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
113 * Check that rte_rib_insert fails gracefully for incorrect user input arguments
116 test_insert_invalid(void)
118 struct rte_rib *rib = NULL;
119 struct rte_rib_node *node, *node1;
120 struct rte_rib_conf config;
121 uint32_t ip = RTE_IPV4(0, 0, 0, 0);
124 config.max_nodes = MAX_RULES;
127 /* rte_rib_insert: rib == NULL */
128 node = rte_rib_insert(NULL, ip, depth);
129 RTE_TEST_ASSERT(node == NULL,
130 "Call succeeded with invalid parameters\n");
132 /*Create valid rib to use in rest of test. */
133 rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
134 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
136 /* rte_rib_insert: depth > MAX_DEPTH */
137 node = rte_rib_insert(rib, ip, MAX_DEPTH + 1);
138 RTE_TEST_ASSERT(node == NULL,
139 "Call succeeded with invalid parameters\n");
141 /* insert the same ip/depth twice*/
142 node = rte_rib_insert(rib, ip, depth);
143 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
144 node1 = rte_rib_insert(rib, ip, depth);
145 RTE_TEST_ASSERT(node1 == NULL,
146 "Call succeeded with invalid parameters\n");
154 * Call rte_rib_node access functions with incorrect input.
155 * After call rte_rib_node access functions with correct args
156 * and check the return values for correctness
161 struct rte_rib *rib = NULL;
162 struct rte_rib_node *node;
163 struct rte_rib_conf config;
165 uint32_t ip = RTE_IPV4(192, 0, 2, 0);
167 uint64_t nh_set = 10;
173 config.max_nodes = MAX_RULES;
176 rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
177 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
179 node = rte_rib_insert(rib, ip, depth);
180 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
182 /* test rte_rib_get_ip() with incorrect args */
183 ret = rte_rib_get_ip(NULL, &ip_ret);
184 RTE_TEST_ASSERT(ret < 0,
185 "Call succeeded with invalid parameters\n");
186 ret = rte_rib_get_ip(node, NULL);
187 RTE_TEST_ASSERT(ret < 0,
188 "Call succeeded with invalid parameters\n");
190 /* test rte_rib_get_depth() with incorrect args */
191 ret = rte_rib_get_depth(NULL, &depth_ret);
192 RTE_TEST_ASSERT(ret < 0,
193 "Call succeeded with invalid parameters\n");
194 ret = rte_rib_get_depth(node, NULL);
195 RTE_TEST_ASSERT(ret < 0,
196 "Call succeeded with invalid parameters\n");
198 /* test rte_rib_set_nh() with incorrect args */
199 ret = rte_rib_set_nh(NULL, nh_set);
200 RTE_TEST_ASSERT(ret < 0,
201 "Call succeeded with invalid parameters\n");
203 /* test rte_rib_get_nh() with incorrect args */
204 ret = rte_rib_get_nh(NULL, &nh_ret);
205 RTE_TEST_ASSERT(ret < 0,
206 "Call succeeded with invalid parameters\n");
207 ret = rte_rib_get_nh(node, NULL);
208 RTE_TEST_ASSERT(ret < 0,
209 "Call succeeded with invalid parameters\n");
211 /* test rte_rib_get_ext() with incorrect args */
212 ext = rte_rib_get_ext(NULL);
213 RTE_TEST_ASSERT(ext == NULL,
214 "Call succeeded with invalid parameters\n");
216 /* check the return values */
217 ret = rte_rib_get_ip(node, &ip_ret);
218 RTE_TEST_ASSERT((ret == 0) && (ip_ret == ip),
219 "Failed to get proper node ip\n");
220 ret = rte_rib_get_depth(node, &depth_ret);
221 RTE_TEST_ASSERT((ret == 0) && (depth_ret == depth),
222 "Failed to get proper node depth\n");
223 ret = rte_rib_set_nh(node, nh_set);
224 RTE_TEST_ASSERT(ret == 0,
225 "Failed to set rte_rib_node nexthop\n");
226 ret = rte_rib_get_nh(node, &nh_ret);
227 RTE_TEST_ASSERT((ret == 0) && (nh_ret == nh_set),
228 "Failed to get proper nexthop\n");
236 * Call insert, lookup/lookup_exact and delete for a single rule
241 struct rte_rib *rib = NULL;
242 struct rte_rib_node *node;
243 struct rte_rib_conf config;
245 uint32_t ip = RTE_IPV4(192, 0, 2, 0);
246 uint64_t next_hop_add = 10;
247 uint64_t next_hop_return;
251 config.max_nodes = MAX_RULES;
254 rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
255 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
257 node = rte_rib_insert(rib, ip, depth);
258 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
260 ret = rte_rib_set_nh(node, next_hop_add);
261 RTE_TEST_ASSERT(ret == 0,
262 "Failed to set rte_rib_node field\n");
264 node = rte_rib_lookup(rib, ip);
265 RTE_TEST_ASSERT(node != NULL, "Failed to lookup\n");
267 ret = rte_rib_get_nh(node, &next_hop_return);
268 RTE_TEST_ASSERT((ret == 0) && (next_hop_add == next_hop_return),
269 "Failed to get proper nexthop\n");
271 node = rte_rib_lookup_exact(rib, ip, depth);
272 RTE_TEST_ASSERT(node != NULL,
273 "Failed to lookup\n");
275 ret = rte_rib_get_nh(node, &next_hop_return);
276 RTE_TEST_ASSERT((ret == 0) && (next_hop_add == next_hop_return),
277 "Failed to get proper nexthop\n");
279 rte_rib_remove(rib, ip, depth);
281 node = rte_rib_lookup(rib, ip);
282 RTE_TEST_ASSERT(node == NULL,
283 "Lookup returns non existent rule\n");
284 node = rte_rib_lookup_exact(rib, ip, depth);
285 RTE_TEST_ASSERT(node == NULL,
286 "Lookup returns non existent rule\n");
294 test_tree_traversal(void)
296 struct rte_rib *rib = NULL;
297 struct rte_rib_node *node;
298 struct rte_rib_conf config;
300 uint32_t ip1 = RTE_IPV4(10, 10, 10, 0);
301 uint32_t ip2 = RTE_IPV4(10, 10, 130, 80);
304 config.max_nodes = MAX_RULES;
307 rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
308 RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
310 node = rte_rib_insert(rib, ip1, depth);
311 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
313 node = rte_rib_insert(rib, ip2, depth);
314 RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
317 node = rte_rib_get_nxt(rib, RTE_IPV4(10, 10, 130, 0), 24, node,
318 RTE_RIB_GET_NXT_ALL);
319 RTE_TEST_ASSERT(node != NULL, "Failed to get rib_node\n");
326 static struct unit_test_suite rib_tests = {
327 .suite_name = "rib autotest",
331 TEST_CASE(test_create_invalid),
332 TEST_CASE(test_free_null),
333 TEST_CASE(test_insert_invalid),
334 TEST_CASE(test_get_fn),
335 TEST_CASE(test_basic),
336 TEST_CASE(test_tree_traversal),
341 static struct unit_test_suite rib_slow_tests = {
342 .suite_name = "rib slow autotest",
346 TEST_CASE(test_multiple_create),
357 return unit_test_suite_runner(&rib_tests);
363 return unit_test_suite_runner(&rib_slow_tests);
366 REGISTER_TEST_COMMAND(rib_autotest, test_rib);
367 REGISTER_TEST_COMMAND(rib_slow_autotest, test_slow_rib);