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