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