blob: 174d299870ca7bbce95883dcf9d0082551c18a80 [file] [log] [blame]
Josh Bailey42ea6852011-07-20 20:44:23 -07001/* $QuaggaId: Format:%an, %ai, %h$ $
2 *
3 * BGP Multipath Unit Test
4 * Copyright (C) 2010 Google Inc.
5 *
6 * This file is part of Quagga
7 *
8 * Quagga is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * Quagga is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Quagga; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * 02111-1307, USA.
22 */
23
24#include <zebra.h>
25
26#include "vty.h"
27#include "stream.h"
28#include "privs.h"
29#include "linklist.h"
30#include "memory.h"
31#include "zclient.h"
Donald Sharp04907292016-01-07 10:03:01 -050032#include "filter.h"
Josh Bailey42ea6852011-07-20 20:44:23 -070033
34#include "bgpd/bgpd.h"
Josh Bailey42ea6852011-07-20 20:44:23 -070035#include "bgpd/bgp_table.h"
36#include "bgpd/bgp_route.h"
Josh Bailey96450fa2011-07-20 20:45:12 -070037#include "bgpd/bgp_attr.h"
38#include "bgpd/bgp_mpath.h"
Josh Bailey42ea6852011-07-20 20:44:23 -070039
40#define VT100_RESET "\x1b[0m"
41#define VT100_RED "\x1b[31m"
42#define VT100_GREEN "\x1b[32m"
43#define VT100_YELLOW "\x1b[33m"
44#define OK VT100_GREEN "OK" VT100_RESET
45#define FAILED VT100_RED "failed" VT100_RESET
46
47#define TEST_PASSED 0
48#define TEST_FAILED -1
49
50#define EXPECT_TRUE(expr, res) \
51 if (!(expr)) \
52 { \
53 printf ("Test failure in %s line %u: %s\n", \
54 __FUNCTION__, __LINE__, #expr); \
55 (res) = TEST_FAILED; \
56 }
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__ {
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;
72};
73
74/* need these to link in libbgp */
75struct thread_master *master = NULL;
76struct zclient *zclient;
77struct zebra_privs_t bgpd_privs =
78{
79 .user = NULL,
80 .group = NULL,
81 .vty_group = NULL,
82};
83
84static int tty = 0;
85
86/* Create fake bgp instance */
87static struct bgp *
88bgp_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 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
107 bgp->rsclient = list_new ();
108 //bgp->rsclient->cmp = (int (*)(void*, void*)) peer_cmp;
109
110 for (afi = AFI_IP; afi < AFI_MAX; afi++)
111 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
112 {
113 bgp->route[afi][safi] = bgp_table_init (afi, safi);
114 bgp->aggregate[afi][safi] = bgp_table_init (afi, safi);
115 bgp->rib[afi][safi] = bgp_table_init (afi, safi);
116 bgp->maxpaths[afi][safi].maxpaths_ebgp = BGP_DEFAULT_MAXPATHS;
117 bgp->maxpaths[afi][safi].maxpaths_ibgp = BGP_DEFAULT_MAXPATHS;
118 }
119
120 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
121 bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
122 bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
123 bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
124 bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
125
126 bgp->as = *as;
127
128 if (name)
129 bgp->name = strdup (name);
130
131 return bgp;
132}
133
134/*=========================================================
135 * Testcase for maximum-paths configuration
136 */
137static int
138setup_bgp_cfg_maximum_paths (testcase_t *t)
139{
140 as_t asn = 1;
141 t->tmp_data = bgp_create_fake (&asn, NULL);
142 if (!t->tmp_data)
143 return -1;
144 return 0;
145}
146
147static int
148run_bgp_cfg_maximum_paths (testcase_t *t)
149{
150 afi_t afi;
151 safi_t safi;
152 struct bgp *bgp;
153 int api_result;
154 int test_result = TEST_PASSED;
155
156 bgp = t->tmp_data;
157 for (afi = AFI_IP; afi < AFI_MAX; afi++)
158 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
159 {
160 /* test bgp_maximum_paths_set */
161 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_EBGP, 10);
162 EXPECT_TRUE (api_result == 0, test_result);
163 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_IBGP, 10);
164 EXPECT_TRUE (api_result == 0, test_result);
165 EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ebgp == 10, test_result);
166 EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ibgp == 10, test_result);
167
168 /* test bgp_maximum_paths_unset */
169 api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_EBGP);
170 EXPECT_TRUE (api_result == 0, test_result);
171 api_result = 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 BGP_DEFAULT_MAXPATHS), test_result);
175 EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ibgp ==
176 BGP_DEFAULT_MAXPATHS), test_result);
177 }
178
179 return test_result;
180}
181
182static int
183cleanup_bgp_cfg_maximum_paths (testcase_t *t)
184{
185 return bgp_delete ((struct bgp *)t->tmp_data);
186}
187
188testcase_t test_bgp_cfg_maximum_paths = {
189 .desc = "Test bgp maximum-paths config",
190 .setup = setup_bgp_cfg_maximum_paths,
191 .run = run_bgp_cfg_maximum_paths,
192 .cleanup = cleanup_bgp_cfg_maximum_paths,
193};
194
195/*=========================================================
Josh Bailey96450fa2011-07-20 20:45:12 -0700196 * Testcase for bgp_mp_list
197 */
Paul Jakma6d4742b2015-11-25 17:14:37 +0000198
199struct bgp test_mp_bgp;
200
Josh Baileyde8d5df2011-07-20 20:46:01 -0700201struct peer test_mp_list_peer[] = {
Paul Jakma6d4742b2015-11-25 17:14:37 +0000202 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
203 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
204 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
205 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
206 { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
Josh Baileyde8d5df2011-07-20 20:46:01 -0700207};
Josh Bailey96450fa2011-07-20 20:45:12 -0700208int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer);
209struct attr test_mp_list_attr[4];
210struct bgp_info test_mp_list_info[] = {
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] },
216};
217int test_mp_list_info_count =
218 sizeof (test_mp_list_info)/sizeof (struct bgp_info);
219
220static int
221setup_bgp_mp_list (testcase_t *t)
222{
223 test_mp_list_attr[0].nexthop.s_addr = 0x01010101;
224 test_mp_list_attr[1].nexthop.s_addr = 0x02020202;
225 test_mp_list_attr[2].nexthop.s_addr = 0x03030303;
226 test_mp_list_attr[3].nexthop.s_addr = 0x04040404;
227
228 if ((test_mp_list_peer[0].su_remote = sockunion_str2su ("1.1.1.1")) == NULL)
229 return -1;
230 if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL)
231 return -1;
232 if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL)
233 return -1;
234 if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL)
235 return -1;
236 if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL)
237 return -1;
238
239 return 0;
240}
241
242static int
243run_bgp_mp_list (testcase_t *t)
244{
245 struct list mp_list;
246 struct listnode *mp_node;
247 struct bgp_info *info;
248 int i;
249 int test_result = TEST_PASSED;
250 bgp_mp_list_init (&mp_list);
251 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
252
253 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
254 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
255 bgp_mp_list_add (&mp_list, &test_mp_list_info[2]);
256 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
257 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
258
David Lamparterc3138952015-04-21 10:02:23 +0200259 for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count;
Josh Bailey96450fa2011-07-20 20:45:12 -0700260 i++, mp_node = listnextnode(mp_node))
261 {
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;
270}
271
272static int
273cleanup_bgp_mp_list (testcase_t *t)
274{
275 int i;
276
277 for (i = 0; i < test_mp_list_peer_count; i++)
278 sockunion_free (test_mp_list_peer[i].su_remote);
279
280 return 0;
281}
282
283testcase_t test_bgp_mp_list = {
284 .desc = "Test bgp_mp_list",
285 .setup = setup_bgp_mp_list,
286 .run = run_bgp_mp_list,
287 .cleanup = cleanup_bgp_mp_list,
288};
289
290/*=========================================================
Josh Baileyde8d5df2011-07-20 20:46:01 -0700291 * Testcase for bgp_info_mpath_update
292 */
293
294struct bgp_node test_rn;
295
296static int
297setup_bgp_info_mpath_update (testcase_t *t)
298{
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;
305}
306
307static int
308run_bgp_info_mpath_update (testcase_t *t)
309{
310 struct bgp_info *new_best, *old_best, *mpath;
311 struct list mp_list;
Paul Jakma6d4742b2015-11-25 17:14:37 +0000312
313 test_mp_bgp.maxpaths[AFI_IP][SAFI_UNICAST].maxpaths_ebgp = 3;
314 test_mp_bgp.maxpaths[AFI_IP][SAFI_UNICAST].maxpaths_ibgp = 3;
315
Josh Baileyde8d5df2011-07-20 20:46:01 -0700316 int test_result = TEST_PASSED;
317 bgp_mp_list_init (&mp_list);
318 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
319 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
320 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
321 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
322 new_best = &test_mp_list_info[3];
323 old_best = NULL;
Paul Jakma6d4742b2015-11-25 17:14:37 +0000324 bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, AFI_IP, SAFI_UNICAST);
Josh Baileyde8d5df2011-07-20 20:46:01 -0700325 bgp_mp_list_clear (&mp_list);
326 EXPECT_TRUE (bgp_info_mpath_count (new_best) == 2, test_result);
327 mpath = bgp_info_mpath_first (new_best);
328 EXPECT_TRUE (mpath == &test_mp_list_info[0], test_result);
329 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
330 mpath = bgp_info_mpath_next (mpath);
331 EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
332 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
333
334 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
335 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
336 new_best = &test_mp_list_info[0];
337 old_best = &test_mp_list_info[3];
Paul Jakma6d4742b2015-11-25 17:14:37 +0000338 bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, AFI_IP, SAFI_UNICAST);
Josh Baileyde8d5df2011-07-20 20:46:01 -0700339 bgp_mp_list_clear (&mp_list);
340 EXPECT_TRUE (bgp_info_mpath_count (new_best) == 1, test_result);
341 mpath = bgp_info_mpath_first (new_best);
342 EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
343 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
344 EXPECT_TRUE (!CHECK_FLAG (test_mp_list_info[0].flags, BGP_INFO_MULTIPATH),
345 test_result);
346
347 return test_result;
348}
349
350static int
351cleanup_bgp_info_mpath_update (testcase_t *t)
352{
353 int i;
354
355 for (i = 0; i < test_mp_list_peer_count; i++)
356 sockunion_free (test_mp_list_peer[i].su_remote);
357
358 return 0;
359}
360
361testcase_t test_bgp_info_mpath_update = {
362 .desc = "Test bgp_info_mpath_update",
363 .setup = setup_bgp_info_mpath_update,
364 .run = run_bgp_info_mpath_update,
365 .cleanup = cleanup_bgp_info_mpath_update,
366};
367
368/*=========================================================
Josh Bailey42ea6852011-07-20 20:44:23 -0700369 * Set up testcase vector
370 */
371testcase_t *all_tests[] = {
372 &test_bgp_cfg_maximum_paths,
Josh Bailey96450fa2011-07-20 20:45:12 -0700373 &test_bgp_mp_list,
Josh Baileyde8d5df2011-07-20 20:46:01 -0700374 &test_bgp_info_mpath_update,
Josh Bailey42ea6852011-07-20 20:44:23 -0700375};
376
377int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *));
378
379/*=========================================================
380 * Test Driver Functions
381 */
382static int
383global_test_init (void)
384{
385 master = thread_master_create ();
Donald Sharp71252932015-09-24 09:25:19 -0400386 zclient = zclient_new (master);
Josh Bailey42ea6852011-07-20 20:44:23 -0700387 bgp_master_init ();
Paul Jakmac9e4f862012-06-14 10:42:39 +0100388 bgp_option_set (BGP_OPT_NO_LISTEN);
389
Josh Bailey42ea6852011-07-20 20:44:23 -0700390 if (fileno (stdout) >= 0)
391 tty = isatty (fileno (stdout));
392 return 0;
393}
394
395static int
396global_test_cleanup (void)
397{
Lou Bergerd5d5e3e2016-01-12 13:41:58 -0500398 if (zclient != NULL)
399 zclient_free (zclient);
Josh Bailey42ea6852011-07-20 20:44:23 -0700400 thread_master_free (master);
401 return 0;
402}
403
404static void
405display_result (testcase_t *test, int result)
406{
407 if (tty)
408 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED);
409 else
410 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED");
411}
412
413static int
414setup_test (testcase_t *t)
415{
416 int res = 0;
417 if (t->setup)
418 res = t->setup (t);
419 return res;
420}
421
422static int
423cleanup_test (testcase_t *t)
424{
425 int res = 0;
426 if (t->cleanup)
427 res = t->cleanup (t);
428 return res;
429}
430
431static void
432run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count)
433{
434 int test_index, result;
435 testcase_t *cur_test;
436
437 *pass_count = *fail_count = 0;
438
439 for (test_index = 0; test_index < num_tests; test_index++)
440 {
441 cur_test = tests[test_index];
442 if (!cur_test->desc)
443 {
444 printf ("error: test %d has no description!\n", test_index);
445 continue;
446 }
447 if (!cur_test->run)
448 {
449 printf ("error: test %s has no run function!\n", cur_test->desc);
450 continue;
451 }
452 if (setup_test (cur_test) != 0)
453 {
454 printf ("error: setup failed for test %s\n", cur_test->desc);
455 continue;
456 }
457 result = cur_test->run (cur_test);
458 if (result == TEST_PASSED)
459 *pass_count += 1;
460 else
461 *fail_count += 1;
462 display_result (cur_test, result);
463 if (cleanup_test (cur_test) != 0)
464 {
465 printf ("error: cleanup failed for test %s\n", cur_test->desc);
466 continue;
467 }
468 }
469}
470
471int
472main (void)
473{
474 int pass_count, fail_count;
475 time_t cur_time;
476
477 time (&cur_time);
478 printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
479 if (global_test_init () != 0)
480 {
481 printf("Global init failed. Terminating.\n");
482 exit(1);
483 }
484 run_tests (all_tests, all_tests_count, &pass_count, &fail_count);
485 global_test_cleanup ();
486 printf("Total pass/fail: %d/%d\n", pass_count, fail_count);
487 return fail_count;
488}