]> git.proxmox.com Git - mirror_frr.git/blob - tests/bgpd/test_mpath.c
Merge pull request #7746 from donaldsharp/eigrp_cleanup
[mirror_frr.git] / tests / bgpd / test_mpath.c
1 /*
2 * BGP Multipath Unit Test
3 * Copyright (C) 2010 Google Inc.
4 *
5 * This file is part of Quagga
6 *
7 * Quagga is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * Quagga is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "qobj.h"
25 #include "vty.h"
26 #include "stream.h"
27 #include "privs.h"
28 #include "linklist.h"
29 #include "memory.h"
30 #include "zclient.h"
31 #include "queue.h"
32 #include "filter.h"
33
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_table.h"
36 #include "bgpd/bgp_route.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_nexthop.h"
39 #include "bgpd/bgp_mpath.h"
40 #include "bgpd/bgp_evpn.h"
41 #include "bgpd/bgp_network.h"
42
43 #define VT100_RESET "\x1b[0m"
44 #define VT100_RED "\x1b[31m"
45 #define VT100_GREEN "\x1b[32m"
46 #define VT100_YELLOW "\x1b[33m"
47 #define OK VT100_GREEN "OK" VT100_RESET
48 #define FAILED VT100_RED "failed" VT100_RESET
49
50 #define TEST_PASSED 0
51 #define TEST_FAILED -1
52
53 #define EXPECT_TRUE(expr, res) \
54 if (!(expr)) { \
55 printf("Test failure in %s line %u: %s\n", __func__, __LINE__, \
56 #expr); \
57 (res) = TEST_FAILED; \
58 }
59
60 typedef struct testcase_t__ testcase_t;
61
62 typedef int (*test_setup_func)(testcase_t *);
63 typedef int (*test_run_func)(testcase_t *);
64 typedef int (*test_cleanup_func)(testcase_t *);
65
66 struct testcase_t__ {
67 const char *desc;
68 void *test_data;
69 void *verify_data;
70 void *tmp_data;
71 test_setup_func setup;
72 test_run_func run;
73 test_cleanup_func cleanup;
74 };
75
76 /* need these to link in libbgp */
77 struct thread_master *master = NULL;
78 extern struct zclient *zclient;
79 struct zebra_privs_t bgpd_privs = {
80 .user = NULL,
81 .group = NULL,
82 .vty_group = NULL,
83 };
84
85 static int tty = 0;
86
87 /* Create fake bgp instance */
88 static struct bgp *bgp_create_fake(as_t *as, const char *name)
89 {
90 struct bgp *bgp;
91 afi_t afi;
92 safi_t safi;
93
94 if ((bgp = XCALLOC(MTYPE_BGP, sizeof(struct bgp))) == NULL)
95 return NULL;
96
97 bgp_lock(bgp);
98 // bgp->peer_self = peer_new (bgp);
99 // bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static
100 // announcement");
101
102 bgp->peer = list_new();
103 // bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
104
105 bgp->group = list_new();
106 // bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp;
107
108 bgp_evpn_init(bgp);
109 FOREACH_AFI_SAFI (afi, safi) {
110 bgp->route[afi][safi] = bgp_table_init(bgp, afi, safi);
111 bgp->aggregate[afi][safi] = bgp_table_init(bgp, afi, safi);
112 bgp->rib[afi][safi] = bgp_table_init(bgp, afi, safi);
113 bgp->maxpaths[afi][safi].maxpaths_ebgp = MULTIPATH_NUM;
114 bgp->maxpaths[afi][safi].maxpaths_ibgp = MULTIPATH_NUM;
115 }
116
117 bgp_scan_init(bgp);
118 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
119 bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
120 bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
121 bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
122 bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
123
124 bgp->as = *as;
125
126 if (name)
127 bgp->name = strdup(name);
128
129 return bgp;
130 }
131
132 /*=========================================================
133 * Testcase for maximum-paths configuration
134 */
135 static int setup_bgp_cfg_maximum_paths(testcase_t *t)
136 {
137 as_t asn = 1;
138 t->tmp_data = bgp_create_fake(&asn, NULL);
139 if (!t->tmp_data)
140 return -1;
141 return 0;
142 }
143
144 static int run_bgp_cfg_maximum_paths(testcase_t *t)
145 {
146 afi_t afi;
147 safi_t safi;
148 struct bgp *bgp;
149 int api_result;
150 int test_result = TEST_PASSED;
151
152 bgp = t->tmp_data;
153 FOREACH_AFI_SAFI (afi, safi) {
154 /* test bgp_maximum_paths_set */
155 api_result = bgp_maximum_paths_set(bgp, afi, safi,
156 BGP_PEER_EBGP, 10, 0);
157 EXPECT_TRUE(api_result == 0, test_result);
158 api_result = bgp_maximum_paths_set(bgp, afi, safi,
159 BGP_PEER_IBGP, 10, 0);
160 EXPECT_TRUE(api_result == 0, test_result);
161 EXPECT_TRUE(bgp->maxpaths[afi][safi].maxpaths_ebgp == 10,
162 test_result);
163 EXPECT_TRUE(bgp->maxpaths[afi][safi].maxpaths_ibgp == 10,
164 test_result);
165
166 /* test bgp_maximum_paths_unset */
167 api_result =
168 bgp_maximum_paths_unset(bgp, afi, safi, BGP_PEER_EBGP);
169 EXPECT_TRUE(api_result == 0, test_result);
170 api_result =
171 bgp_maximum_paths_unset(bgp, afi, safi, BGP_PEER_IBGP);
172 EXPECT_TRUE(api_result == 0, test_result);
173 EXPECT_TRUE((bgp->maxpaths[afi][safi].maxpaths_ebgp
174 == MULTIPATH_NUM),
175 test_result);
176 EXPECT_TRUE((bgp->maxpaths[afi][safi].maxpaths_ibgp
177 == MULTIPATH_NUM),
178 test_result);
179 }
180
181 return test_result;
182 }
183
184 static int cleanup_bgp_cfg_maximum_paths(testcase_t *t)
185 {
186 return bgp_delete((struct bgp *)t->tmp_data);
187 }
188
189 testcase_t test_bgp_cfg_maximum_paths = {
190 .desc = "Test bgp maximum-paths config",
191 .setup = setup_bgp_cfg_maximum_paths,
192 .run = run_bgp_cfg_maximum_paths,
193 .cleanup = cleanup_bgp_cfg_maximum_paths,
194 };
195
196 /*=========================================================
197 * Testcase for bgp_mp_list
198 */
199 struct peer test_mp_list_peer[] = {
200 {.local_as = 1, .as = 2}, {.local_as = 1, .as = 2},
201 {.local_as = 1, .as = 2}, {.local_as = 1, .as = 2},
202 {.local_as = 1, .as = 2},
203 };
204 int test_mp_list_peer_count = array_size(test_mp_list_peer);
205 struct attr test_mp_list_attr[4];
206 struct bgp_path_info test_mp_list_info[] = {
207 {.peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0]},
208 {.peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1]},
209 {.peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1]},
210 {.peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2]},
211 {.peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3]},
212 };
213 int test_mp_list_info_count = array_size(test_mp_list_info);
214
215 static int setup_bgp_mp_list(testcase_t *t)
216 {
217 test_mp_list_attr[0].nexthop.s_addr = 0x01010101;
218 test_mp_list_attr[1].nexthop.s_addr = 0x02020202;
219 test_mp_list_attr[2].nexthop.s_addr = 0x03030303;
220 test_mp_list_attr[3].nexthop.s_addr = 0x04040404;
221
222 if ((test_mp_list_peer[0].su_remote = sockunion_str2su("1.1.1.1"))
223 == NULL)
224 return -1;
225 if ((test_mp_list_peer[1].su_remote = sockunion_str2su("2.2.2.2"))
226 == NULL)
227 return -1;
228 if ((test_mp_list_peer[2].su_remote = sockunion_str2su("3.3.3.3"))
229 == NULL)
230 return -1;
231 if ((test_mp_list_peer[3].su_remote = sockunion_str2su("4.4.4.4"))
232 == NULL)
233 return -1;
234 if ((test_mp_list_peer[4].su_remote = sockunion_str2su("5.5.5.5"))
235 == NULL)
236 return -1;
237
238 return 0;
239 }
240
241 static int run_bgp_mp_list(testcase_t *t)
242 {
243 struct list mp_list;
244 struct listnode *mp_node;
245 struct bgp_path_info *info;
246 int i;
247 int test_result = TEST_PASSED;
248 bgp_mp_list_init(&mp_list);
249 EXPECT_TRUE(listcount(&mp_list) == 0, test_result);
250
251 bgp_mp_list_add(&mp_list, &test_mp_list_info[1]);
252 bgp_mp_list_add(&mp_list, &test_mp_list_info[4]);
253 bgp_mp_list_add(&mp_list, &test_mp_list_info[2]);
254 bgp_mp_list_add(&mp_list, &test_mp_list_info[3]);
255 bgp_mp_list_add(&mp_list, &test_mp_list_info[0]);
256
257 for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count;
258 i++, mp_node = listnextnode(mp_node)) {
259 info = listgetdata(mp_node);
260 EXPECT_TRUE(info == &test_mp_list_info[i], test_result);
261 }
262
263 bgp_mp_list_clear(&mp_list);
264 EXPECT_TRUE(listcount(&mp_list) == 0, test_result);
265
266 return test_result;
267 }
268
269 static int cleanup_bgp_mp_list(testcase_t *t)
270 {
271 int i;
272
273 for (i = 0; i < test_mp_list_peer_count; i++)
274 sockunion_free(test_mp_list_peer[i].su_remote);
275
276 return 0;
277 }
278
279 testcase_t test_bgp_mp_list = {
280 .desc = "Test bgp_mp_list",
281 .setup = setup_bgp_mp_list,
282 .run = run_bgp_mp_list,
283 .cleanup = cleanup_bgp_mp_list,
284 };
285
286 /*=========================================================
287 * Testcase for bgp_path_info_mpath_update
288 */
289
290 struct bgp_node test_rn;
291
292 static int setup_bgp_path_info_mpath_update(testcase_t *t)
293 {
294 int i;
295 struct bgp *bgp;
296 struct bgp_table *rt;
297 struct route_node *rt_node;
298 as_t asn = 1;
299
300 t->tmp_data = bgp_create_fake(&asn, NULL);
301 if (!t->tmp_data)
302 return -1;
303
304 bgp = t->tmp_data;
305 rt = bgp->rib[AFI_IP][SAFI_UNICAST];
306
307 if (!rt)
308 return -1;
309
310 str2prefix("42.1.1.0/24", &test_rn.p);
311 rt_node = bgp_dest_to_rnode(&test_rn);
312 memcpy((struct route_table *)&rt_node->table, &rt->route_table,
313 sizeof(struct route_table *));
314 setup_bgp_mp_list(t);
315 for (i = 0; i < test_mp_list_info_count; i++)
316 bgp_path_info_add(&test_rn, &test_mp_list_info[i]);
317 return 0;
318 }
319
320 static int run_bgp_path_info_mpath_update(testcase_t *t)
321 {
322 struct bgp_path_info *new_best, *old_best, *mpath;
323 struct list mp_list;
324 struct bgp_maxpaths_cfg mp_cfg = {3, 3};
325 int test_result = TEST_PASSED;
326 bgp_mp_list_init(&mp_list);
327 bgp_mp_list_add(&mp_list, &test_mp_list_info[4]);
328 bgp_mp_list_add(&mp_list, &test_mp_list_info[3]);
329 bgp_mp_list_add(&mp_list, &test_mp_list_info[0]);
330 bgp_mp_list_add(&mp_list, &test_mp_list_info[1]);
331 new_best = &test_mp_list_info[3];
332 old_best = NULL;
333 bgp_path_info_mpath_update(&test_rn, new_best, old_best, &mp_list,
334 &mp_cfg);
335 bgp_mp_list_clear(&mp_list);
336 EXPECT_TRUE(bgp_path_info_mpath_count(new_best) == 2, test_result);
337 mpath = bgp_path_info_mpath_first(new_best);
338 EXPECT_TRUE(mpath == &test_mp_list_info[0], test_result);
339 EXPECT_TRUE(CHECK_FLAG(mpath->flags, BGP_PATH_MULTIPATH), test_result);
340 mpath = bgp_path_info_mpath_next(mpath);
341 EXPECT_TRUE(mpath == &test_mp_list_info[1], test_result);
342 EXPECT_TRUE(CHECK_FLAG(mpath->flags, BGP_PATH_MULTIPATH), test_result);
343
344 bgp_mp_list_add(&mp_list, &test_mp_list_info[0]);
345 bgp_mp_list_add(&mp_list, &test_mp_list_info[1]);
346 new_best = &test_mp_list_info[0];
347 old_best = &test_mp_list_info[3];
348 bgp_path_info_mpath_update(&test_rn, new_best, old_best, &mp_list,
349 &mp_cfg);
350 bgp_mp_list_clear(&mp_list);
351 EXPECT_TRUE(bgp_path_info_mpath_count(new_best) == 1, test_result);
352 mpath = bgp_path_info_mpath_first(new_best);
353 EXPECT_TRUE(mpath == &test_mp_list_info[1], test_result);
354 EXPECT_TRUE(CHECK_FLAG(mpath->flags, BGP_PATH_MULTIPATH), test_result);
355 EXPECT_TRUE(!CHECK_FLAG(test_mp_list_info[0].flags, BGP_PATH_MULTIPATH),
356 test_result);
357
358 return test_result;
359 }
360
361 static int cleanup_bgp_path_info_mpath_update(testcase_t *t)
362 {
363 int i;
364
365 for (i = 0; i < test_mp_list_peer_count; i++)
366 sockunion_free(test_mp_list_peer[i].su_remote);
367
368 return bgp_delete((struct bgp *)t->tmp_data);
369 }
370
371 testcase_t test_bgp_path_info_mpath_update = {
372 .desc = "Test bgp_path_info_mpath_update",
373 .setup = setup_bgp_path_info_mpath_update,
374 .run = run_bgp_path_info_mpath_update,
375 .cleanup = cleanup_bgp_path_info_mpath_update,
376 };
377
378 /*=========================================================
379 * Set up testcase vector
380 */
381 testcase_t *all_tests[] = {
382 &test_bgp_cfg_maximum_paths, &test_bgp_mp_list,
383 &test_bgp_path_info_mpath_update,
384 };
385
386 int all_tests_count = array_size(all_tests);
387
388 /*=========================================================
389 * Test Driver Functions
390 */
391 static int global_test_init(void)
392 {
393 qobj_init();
394 master = thread_master_create(NULL);
395 zclient = zclient_new(master, &zclient_options_default);
396 bgp_master_init(master, BGP_SOCKET_SNDBUF_SIZE, list_new());
397 vrf_init(NULL, NULL, NULL, NULL, NULL);
398 bgp_option_set(BGP_OPT_NO_LISTEN);
399
400 if (fileno(stdout) >= 0)
401 tty = isatty(fileno(stdout));
402 return 0;
403 }
404
405 static int global_test_cleanup(void)
406 {
407 if (zclient != NULL)
408 zclient_free(zclient);
409 thread_master_free(master);
410 return 0;
411 }
412
413 static void display_result(testcase_t *test, int result)
414 {
415 if (tty)
416 printf("%s: %s\n", test->desc,
417 result == TEST_PASSED ? OK : FAILED);
418 else
419 printf("%s: %s\n", test->desc,
420 result == TEST_PASSED ? "OK" : "FAILED");
421 }
422
423 static int setup_test(testcase_t *t)
424 {
425 int res = 0;
426 if (t->setup)
427 res = t->setup(t);
428 return res;
429 }
430
431 static int cleanup_test(testcase_t *t)
432 {
433 int res = 0;
434 if (t->cleanup)
435 res = t->cleanup(t);
436 return res;
437 }
438
439 static void run_tests(testcase_t *tests[], int num_tests, int *pass_count,
440 int *fail_count)
441 {
442 int test_index, result;
443 testcase_t *cur_test;
444
445 *pass_count = *fail_count = 0;
446
447 for (test_index = 0; test_index < num_tests; test_index++) {
448 cur_test = tests[test_index];
449 if (!cur_test->desc) {
450 printf("error: test %d has no description!\n",
451 test_index);
452 continue;
453 }
454 if (!cur_test->run) {
455 printf("error: test %s has no run function!\n",
456 cur_test->desc);
457 continue;
458 }
459 if (setup_test(cur_test) != 0) {
460 printf("error: setup failed for test %s\n",
461 cur_test->desc);
462 continue;
463 }
464 result = cur_test->run(cur_test);
465 if (result == TEST_PASSED)
466 *pass_count += 1;
467 else
468 *fail_count += 1;
469 display_result(cur_test, result);
470 if (cleanup_test(cur_test) != 0) {
471 printf("error: cleanup failed for test %s\n",
472 cur_test->desc);
473 continue;
474 }
475 }
476 }
477
478 int main(void)
479 {
480 int pass_count, fail_count;
481 time_t cur_time;
482
483 time(&cur_time);
484 printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
485 if (global_test_init() != 0) {
486 printf("Global init failed. Terminating.\n");
487 exit(1);
488 }
489 run_tests(all_tests, all_tests_count, &pass_count, &fail_count);
490 global_test_cleanup();
491 printf("Total pass/fail: %d/%d\n", pass_count, fail_count);
492 return fail_count;
493 }