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