]> git.proxmox.com Git - mirror_frr.git/blame - tests/bgp_mpath_test.c
Makfile issues to compile better
[mirror_frr.git] / tests / bgp_mpath_test.c
CommitLineData
42ea6851
JB
1/* $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"
3f9c7369 32#include "queue.h"
42ea6851
JB
33
34#include "bgpd/bgpd.h"
42ea6851
JB
35#include "bgpd/bgp_table.h"
36#include "bgpd/bgp_route.h"
96450faf
JB
37#include "bgpd/bgp_attr.h"
38#include "bgpd/bgp_mpath.h"
42ea6851
JB
39
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
42ea6851
JB
107 for (afi = AFI_IP; afi < AFI_MAX; afi++)
108 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
109 {
110 bgp->route[afi][safi] = bgp_table_init (afi, safi);
111 bgp->aggregate[afi][safi] = bgp_table_init (afi, safi);
112 bgp->rib[afi][safi] = bgp_table_init (afi, safi);
d5b77cc2
DS
113 bgp->maxpaths[afi][safi].maxpaths_ebgp = MULTIPATH_NUM;
114 bgp->maxpaths[afi][safi].maxpaths_ibgp = MULTIPATH_NUM;
42ea6851
JB
115 }
116
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;
129}
130
131/*=========================================================
132 * Testcase for maximum-paths configuration
133 */
134static int
135setup_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
144static int
145run_bgp_cfg_maximum_paths (testcase_t *t)
146{
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 {
157 /* test bgp_maximum_paths_set */
5e242b0d 158 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_EBGP, 10, 0);
42ea6851 159 EXPECT_TRUE (api_result == 0, test_result);
5e242b0d 160 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_IBGP, 10, 0);
42ea6851
JB
161 EXPECT_TRUE (api_result == 0, test_result);
162 EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ebgp == 10, test_result);
163 EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ibgp == 10, test_result);
164
165 /* test bgp_maximum_paths_unset */
166 api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_EBGP);
167 EXPECT_TRUE (api_result == 0, test_result);
168 api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_IBGP);
169 EXPECT_TRUE (api_result == 0, test_result);
170 EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ebgp ==
d5b77cc2 171 MULTIPATH_NUM), test_result);
42ea6851 172 EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ibgp ==
d5b77cc2 173 MULTIPATH_NUM), test_result);
42ea6851
JB
174 }
175
176 return test_result;
177}
178
179static int
180cleanup_bgp_cfg_maximum_paths (testcase_t *t)
181{
182 return bgp_delete ((struct bgp *)t->tmp_data);
183}
184
185testcase_t test_bgp_cfg_maximum_paths = {
186 .desc = "Test bgp maximum-paths config",
187 .setup = setup_bgp_cfg_maximum_paths,
188 .run = run_bgp_cfg_maximum_paths,
189 .cleanup = cleanup_bgp_cfg_maximum_paths,
190};
191
96450faf
JB
192/*=========================================================
193 * Testcase for bgp_mp_list
194 */
de8d5dff
JB
195struct peer test_mp_list_peer[] = {
196 { .local_as = 1, .as = 2 },
197 { .local_as = 1, .as = 2 },
198 { .local_as = 1, .as = 2 },
199 { .local_as = 1, .as = 2 },
200 { .local_as = 1, .as = 2 },
201};
96450faf
JB
202int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer);
203struct attr test_mp_list_attr[4];
204struct bgp_info test_mp_list_info[] = {
205 { .peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0] },
206 { .peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1] },
207 { .peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1] },
208 { .peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2] },
209 { .peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3] },
210};
211int test_mp_list_info_count =
212 sizeof (test_mp_list_info)/sizeof (struct bgp_info);
213
214static int
215setup_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")) == NULL)
223 return -1;
224 if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL)
225 return -1;
226 if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL)
227 return -1;
228 if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL)
229 return -1;
230 if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL)
231 return -1;
232
233 return 0;
234}
235
236static int
237run_bgp_mp_list (testcase_t *t)
238{
239 struct list mp_list;
240 struct listnode *mp_node;
241 struct bgp_info *info;
242 int i;
243 int test_result = TEST_PASSED;
244 bgp_mp_list_init (&mp_list);
245 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
246
247 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
248 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
249 bgp_mp_list_add (&mp_list, &test_mp_list_info[2]);
250 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
251 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
252
253 for (i = 0, mp_node = listhead(&mp_list); i < test_mp_list_info_count;
254 i++, mp_node = listnextnode(mp_node))
255 {
256 info = listgetdata(mp_node);
257 EXPECT_TRUE (info == &test_mp_list_info[i], test_result);
258 }
259
260 bgp_mp_list_clear (&mp_list);
261 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
262
263 return test_result;
264}
265
266static int
267cleanup_bgp_mp_list (testcase_t *t)
268{
269 int i;
270
271 for (i = 0; i < test_mp_list_peer_count; i++)
272 sockunion_free (test_mp_list_peer[i].su_remote);
273
274 return 0;
275}
276
277testcase_t test_bgp_mp_list = {
278 .desc = "Test bgp_mp_list",
279 .setup = setup_bgp_mp_list,
280 .run = run_bgp_mp_list,
281 .cleanup = cleanup_bgp_mp_list,
282};
283
de8d5dff
JB
284/*=========================================================
285 * Testcase for bgp_info_mpath_update
286 */
287
288struct bgp_node test_rn;
289
290static int
291setup_bgp_info_mpath_update (testcase_t *t)
292{
293 int i;
294 str2prefix ("42.1.1.0/24", &test_rn.p);
295 setup_bgp_mp_list (t);
296 for (i = 0; i < test_mp_list_info_count; i++)
297 bgp_info_add (&test_rn, &test_mp_list_info[i]);
298 return 0;
299}
300
301static int
302run_bgp_info_mpath_update (testcase_t *t)
303{
304 struct bgp_info *new_best, *old_best, *mpath;
305 struct list mp_list;
306 struct bgp_maxpaths_cfg mp_cfg = { 3, 3 };
307 int test_result = TEST_PASSED;
308 bgp_mp_list_init (&mp_list);
309 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
310 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
311 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
312 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
313 new_best = &test_mp_list_info[3];
314 old_best = NULL;
315 bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, &mp_cfg);
316 bgp_mp_list_clear (&mp_list);
317 EXPECT_TRUE (bgp_info_mpath_count (new_best) == 2, test_result);
318 mpath = bgp_info_mpath_first (new_best);
319 EXPECT_TRUE (mpath == &test_mp_list_info[0], test_result);
320 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
321 mpath = bgp_info_mpath_next (mpath);
322 EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
323 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
324
325 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
326 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
327 new_best = &test_mp_list_info[0];
328 old_best = &test_mp_list_info[3];
329 bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, &mp_cfg);
330 bgp_mp_list_clear (&mp_list);
331 EXPECT_TRUE (bgp_info_mpath_count (new_best) == 1, test_result);
332 mpath = bgp_info_mpath_first (new_best);
333 EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
334 EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
335 EXPECT_TRUE (!CHECK_FLAG (test_mp_list_info[0].flags, BGP_INFO_MULTIPATH),
336 test_result);
337
338 return test_result;
339}
340
341static int
342cleanup_bgp_info_mpath_update (testcase_t *t)
343{
344 int i;
345
346 for (i = 0; i < test_mp_list_peer_count; i++)
347 sockunion_free (test_mp_list_peer[i].su_remote);
348
349 return 0;
350}
351
352testcase_t test_bgp_info_mpath_update = {
353 .desc = "Test bgp_info_mpath_update",
354 .setup = setup_bgp_info_mpath_update,
355 .run = run_bgp_info_mpath_update,
356 .cleanup = cleanup_bgp_info_mpath_update,
357};
358
42ea6851
JB
359/*=========================================================
360 * Set up testcase vector
361 */
362testcase_t *all_tests[] = {
363 &test_bgp_cfg_maximum_paths,
96450faf 364 &test_bgp_mp_list,
de8d5dff 365 &test_bgp_info_mpath_update,
42ea6851
JB
366};
367
368int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *));
369
370/*=========================================================
371 * Test Driver Functions
372 */
373static int
374global_test_init (void)
375{
376 master = thread_master_create ();
4140ca4d 377 zclient = zclient_new(master);
42ea6851 378 bgp_master_init ();
c9e4f862
PJ
379 bgp_option_set (BGP_OPT_NO_LISTEN);
380
42ea6851
JB
381 if (fileno (stdout) >= 0)
382 tty = isatty (fileno (stdout));
383 return 0;
384}
385
386static int
387global_test_cleanup (void)
388{
389 zclient_free (zclient);
390 thread_master_free (master);
391 return 0;
392}
393
394static void
395display_result (testcase_t *test, int result)
396{
397 if (tty)
398 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED);
399 else
400 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED");
401}
402
403static int
404setup_test (testcase_t *t)
405{
406 int res = 0;
407 if (t->setup)
408 res = t->setup (t);
409 return res;
410}
411
412static int
413cleanup_test (testcase_t *t)
414{
415 int res = 0;
416 if (t->cleanup)
417 res = t->cleanup (t);
418 return res;
419}
420
421static void
422run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count)
423{
424 int test_index, result;
425 testcase_t *cur_test;
426
427 *pass_count = *fail_count = 0;
428
429 for (test_index = 0; test_index < num_tests; test_index++)
430 {
431 cur_test = tests[test_index];
432 if (!cur_test->desc)
433 {
434 printf ("error: test %d has no description!\n", test_index);
435 continue;
436 }
437 if (!cur_test->run)
438 {
439 printf ("error: test %s has no run function!\n", cur_test->desc);
440 continue;
441 }
442 if (setup_test (cur_test) != 0)
443 {
444 printf ("error: setup failed for test %s\n", 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 {
455 printf ("error: cleanup failed for test %s\n", cur_test->desc);
456 continue;
457 }
458 }
459}
460
461int
462main (void)
463{
464 int pass_count, fail_count;
465 time_t cur_time;
466
467 time (&cur_time);
468 printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
469 if (global_test_init () != 0)
470 {
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;
478}