1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
* Copyright(c) 2019 Intel Corporation
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <rte_ip.h>
#include <rte_rib.h>
#include "test.h"
typedef int32_t (*rte_rib_test)(void);
static int32_t test_create_invalid(void);
static int32_t test_multiple_create(void);
static int32_t test_free_null(void);
static int32_t test_insert_invalid(void);
static int32_t test_get_fn(void);
static int32_t test_basic(void);
static int32_t test_tree_traversal(void);
#define MAX_DEPTH 32
#define MAX_RULES (1 << 22)
/*
* Check that rte_rib_create fails gracefully for incorrect user input
* arguments
*/
int32_t
test_create_invalid(void)
{
struct rte_rib *rib = NULL;
struct rte_rib_conf config;
config.max_nodes = MAX_RULES;
config.ext_sz = 0;
/* rte_rib_create: rib name == NULL */
rib = rte_rib_create(NULL, SOCKET_ID_ANY, &config);
RTE_TEST_ASSERT(rib == NULL,
"Call succeeded with invalid parameters\n");
/* rte_rib_create: config == NULL */
rib = rte_rib_create(__func__, SOCKET_ID_ANY, NULL);
RTE_TEST_ASSERT(rib == NULL,
"Call succeeded with invalid parameters\n");
/* socket_id < -1 is invalid */
rib = rte_rib_create(__func__, -2, &config);
RTE_TEST_ASSERT(rib == NULL,
"Call succeeded with invalid parameters\n");
/* rte_rib_create: max_nodes = 0 */
config.max_nodes = 0;
rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
RTE_TEST_ASSERT(rib == NULL,
"Call succeeded with invalid parameters\n");
config.max_nodes = MAX_RULES;
return TEST_SUCCESS;
}
/*
* Create rib table then delete rib table 10 times
* Use a slightly different rules size each time
*/
int32_t
test_multiple_create(void)
{
struct rte_rib *rib = NULL;
struct rte_rib_conf config;
int32_t i;
config.ext_sz = 0;
for (i = 0; i < 100; i++) {
config.max_nodes = MAX_RULES - i;
rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
rte_rib_free(rib);
}
/* Can not test free so return success */
return TEST_SUCCESS;
}
/*
* Call rte_rib_free for NULL pointer user input. Note: free has no return and
* therefore it is impossible to check for failure but this test is added to
* increase function coverage metrics and to validate that freeing null does
* not crash.
*/
int32_t
test_free_null(void)
{
struct rte_rib *rib = NULL;
struct rte_rib_conf config;
config.max_nodes = MAX_RULES;
config.ext_sz = 0;
rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
rte_rib_free(rib);
rte_rib_free(NULL);
return TEST_SUCCESS;
}
/*
* Check that rte_rib_insert fails gracefully for incorrect user input arguments
*/
int32_t
test_insert_invalid(void)
{
struct rte_rib *rib = NULL;
struct rte_rib_node *node, *node1;
struct rte_rib_conf config;
uint32_t ip = RTE_IPV4(0, 0, 0, 0);
uint8_t depth = 24;
config.max_nodes = MAX_RULES;
config.ext_sz = 0;
/* rte_rib_insert: rib == NULL */
node = rte_rib_insert(NULL, ip, depth);
RTE_TEST_ASSERT(node == NULL,
"Call succeeded with invalid parameters\n");
/*Create valid rib to use in rest of test. */
rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
/* rte_rib_insert: depth > MAX_DEPTH */
node = rte_rib_insert(rib, ip, MAX_DEPTH + 1);
RTE_TEST_ASSERT(node == NULL,
"Call succeeded with invalid parameters\n");
/* insert the same ip/depth twice*/
node = rte_rib_insert(rib, ip, depth);
RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
node1 = rte_rib_insert(rib, ip, depth);
RTE_TEST_ASSERT(node1 == NULL,
"Call succeeded with invalid parameters\n");
rte_rib_free(rib);
return TEST_SUCCESS;
}
/*
* Call rte_rib_node access functions with incorrect input.
* After call rte_rib_node access functions with correct args
* and check the return values for correctness
*/
int32_t
test_get_fn(void)
{
struct rte_rib *rib = NULL;
struct rte_rib_node *node;
struct rte_rib_conf config;
void *ext;
uint32_t ip = RTE_IPV4(192, 0, 2, 0);
uint32_t ip_ret;
uint64_t nh_set = 10;
uint64_t nh_ret;
uint8_t depth = 24;
uint8_t depth_ret;
int ret;
config.max_nodes = MAX_RULES;
config.ext_sz = 0;
rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
node = rte_rib_insert(rib, ip, depth);
RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
/* test rte_rib_get_ip() with incorrect args */
ret = rte_rib_get_ip(NULL, &ip_ret);
RTE_TEST_ASSERT(ret < 0,
"Call succeeded with invalid parameters\n");
ret = rte_rib_get_ip(node, NULL);
RTE_TEST_ASSERT(ret < 0,
"Call succeeded with invalid parameters\n");
/* test rte_rib_get_depth() with incorrect args */
ret = rte_rib_get_depth(NULL, &depth_ret);
RTE_TEST_ASSERT(ret < 0,
"Call succeeded with invalid parameters\n");
ret = rte_rib_get_depth(node, NULL);
RTE_TEST_ASSERT(ret < 0,
"Call succeeded with invalid parameters\n");
/* test rte_rib_set_nh() with incorrect args */
ret = rte_rib_set_nh(NULL, nh_set);
RTE_TEST_ASSERT(ret < 0,
"Call succeeded with invalid parameters\n");
/* test rte_rib_get_nh() with incorrect args */
ret = rte_rib_get_nh(NULL, &nh_ret);
RTE_TEST_ASSERT(ret < 0,
"Call succeeded with invalid parameters\n");
ret = rte_rib_get_nh(node, NULL);
RTE_TEST_ASSERT(ret < 0,
"Call succeeded with invalid parameters\n");
/* test rte_rib_get_ext() with incorrect args */
ext = rte_rib_get_ext(NULL);
RTE_TEST_ASSERT(ext == NULL,
"Call succeeded with invalid parameters\n");
/* check the return values */
ret = rte_rib_get_ip(node, &ip_ret);
RTE_TEST_ASSERT((ret == 0) && (ip_ret == ip),
"Failed to get proper node ip\n");
ret = rte_rib_get_depth(node, &depth_ret);
RTE_TEST_ASSERT((ret == 0) && (depth_ret == depth),
"Failed to get proper node depth\n");
ret = rte_rib_set_nh(node, nh_set);
RTE_TEST_ASSERT(ret == 0,
"Failed to set rte_rib_node nexthop\n");
ret = rte_rib_get_nh(node, &nh_ret);
RTE_TEST_ASSERT((ret == 0) && (nh_ret == nh_set),
"Failed to get proper nexthop\n");
rte_rib_free(rib);
return TEST_SUCCESS;
}
/*
* Call insert, lookup/lookup_exact and delete for a single rule
*/
int32_t
test_basic(void)
{
struct rte_rib *rib = NULL;
struct rte_rib_node *node;
struct rte_rib_conf config;
uint32_t ip = RTE_IPV4(192, 0, 2, 0);
uint64_t next_hop_add = 10;
uint64_t next_hop_return;
uint8_t depth = 24;
int ret;
config.max_nodes = MAX_RULES;
config.ext_sz = 0;
rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
node = rte_rib_insert(rib, ip, depth);
RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
ret = rte_rib_set_nh(node, next_hop_add);
RTE_TEST_ASSERT(ret == 0,
"Failed to set rte_rib_node field\n");
node = rte_rib_lookup(rib, ip);
RTE_TEST_ASSERT(node != NULL, "Failed to lookup\n");
ret = rte_rib_get_nh(node, &next_hop_return);
RTE_TEST_ASSERT((ret == 0) && (next_hop_add == next_hop_return),
"Failed to get proper nexthop\n");
node = rte_rib_lookup_exact(rib, ip, depth);
RTE_TEST_ASSERT(node != NULL,
"Failed to lookup\n");
ret = rte_rib_get_nh(node, &next_hop_return);
RTE_TEST_ASSERT((ret == 0) && (next_hop_add == next_hop_return),
"Failed to get proper nexthop\n");
rte_rib_remove(rib, ip, depth);
node = rte_rib_lookup(rib, ip);
RTE_TEST_ASSERT(node == NULL,
"Lookup returns non existent rule\n");
node = rte_rib_lookup_exact(rib, ip, depth);
RTE_TEST_ASSERT(node == NULL,
"Lookup returns non existent rule\n");
rte_rib_free(rib);
return TEST_SUCCESS;
}
int32_t
test_tree_traversal(void)
{
struct rte_rib *rib = NULL;
struct rte_rib_node *node;
struct rte_rib_conf config;
uint32_t ip1 = RTE_IPV4(10, 10, 10, 0);
uint32_t ip2 = RTE_IPV4(10, 10, 130, 80);
uint8_t depth = 30;
config.max_nodes = MAX_RULES;
config.ext_sz = 0;
rib = rte_rib_create(__func__, SOCKET_ID_ANY, &config);
RTE_TEST_ASSERT(rib != NULL, "Failed to create RIB\n");
node = rte_rib_insert(rib, ip1, depth);
RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
node = rte_rib_insert(rib, ip2, depth);
RTE_TEST_ASSERT(node != NULL, "Failed to insert rule\n");
node = NULL;
node = rte_rib_get_nxt(rib, RTE_IPV4(10, 10, 130, 0), 24, node,
RTE_RIB_GET_NXT_ALL);
RTE_TEST_ASSERT(node != NULL, "Failed to get rib_node\n");
rte_rib_free(rib);
return TEST_SUCCESS;
}
static struct unit_test_suite rib_tests = {
.suite_name = "rib autotest",
.setup = NULL,
.teardown = NULL,
.unit_test_cases = {
TEST_CASE(test_create_invalid),
TEST_CASE(test_free_null),
TEST_CASE(test_insert_invalid),
TEST_CASE(test_get_fn),
TEST_CASE(test_basic),
TEST_CASE(test_tree_traversal),
TEST_CASES_END()
}
};
static struct unit_test_suite rib_slow_tests = {
.suite_name = "rib slow autotest",
.setup = NULL,
.teardown = NULL,
.unit_test_cases = {
TEST_CASE(test_multiple_create),
TEST_CASES_END()
}
};
/*
* Do all unit tests.
*/
static int
test_rib(void)
{
return unit_test_suite_runner(&rib_tests);
}
static int
test_slow_rib(void)
{
return unit_test_suite_runner(&rib_slow_tests);
}
REGISTER_TEST_COMMAND(rib_autotest, test_rib);
REGISTER_TEST_COMMAND(rib_slow_autotest, test_slow_rib);
|