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