]> git.proxmox.com Git - mirror_frr.git/blob - tests/bgpd/test_mpath.c
*: add frr_init() infrastructure
[mirror_frr.git] / tests / bgpd / test_mpath.c
1 /*
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 *
17 * You should have received a copy of the GNU General Public License
18 * along with Quagga; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23 #include <zebra.h>
24
25 #include "qobj.h"
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 #include "queue.h"
33 #include "filter.h"
34
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_nexthop.h"
40 #include "bgpd/bgp_mpath.h"
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
52 #define EXPECT_TRUE(expr, res) \
53 if (!(expr)) \
54 { \
55 printf ("Test failure in %s line %u: %s\n", \
56 __FUNCTION__, __LINE__, #expr); \
57 (res) = TEST_FAILED; \
58 }
59
60 typedef struct testcase_t__ testcase_t;
61
62 typedef int (*test_setup_func)(testcase_t *);
63 typedef int (*test_run_func)(testcase_t *);
64 typedef int (*test_cleanup_func)(testcase_t *);
65
66 struct testcase_t__ {
67 const char *desc;
68 void *test_data;
69 void *verify_data;
70 void *tmp_data;
71 test_setup_func setup;
72 test_run_func run;
73 test_cleanup_func cleanup;
74 };
75
76 /* need these to link in libbgp */
77 struct thread_master *master = NULL;
78 struct zclient *zclient;
79 struct zebra_privs_t bgpd_privs =
80 {
81 .user = NULL,
82 .group = NULL,
83 .vty_group = NULL,
84 };
85
86 static int tty = 0;
87
88 /* Create fake bgp instance */
89 static struct bgp *
90 bgp_create_fake (as_t *as, const char *name)
91 {
92 struct bgp *bgp;
93 afi_t afi;
94 safi_t safi;
95
96 if ( (bgp = XCALLOC (MTYPE_BGP, sizeof (struct bgp))) == NULL)
97 return NULL;
98
99 bgp_lock (bgp);
100 //bgp->peer_self = peer_new (bgp);
101 //bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static announcement");
102
103 bgp->peer = list_new ();
104 //bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
105
106 bgp->group = list_new ();
107 //bgp->group->cmp = (int (*)(void *, void *)) peer_group_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 = MULTIPATH_NUM;
116 bgp->maxpaths[afi][safi].maxpaths_ibgp = MULTIPATH_NUM;
117 }
118
119 bgp_scan_init (bgp);
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 */
137 static int
138 setup_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
147 static int
148 run_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, 0);
162 EXPECT_TRUE (api_result == 0, test_result);
163 api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_IBGP, 10, 0);
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 MULTIPATH_NUM), test_result);
175 EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ibgp ==
176 MULTIPATH_NUM), test_result);
177 }
178
179 return test_result;
180 }
181
182 static int
183 cleanup_bgp_cfg_maximum_paths (testcase_t *t)
184 {
185 return bgp_delete ((struct bgp *)t->tmp_data);
186 }
187
188 testcase_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 /*=========================================================
196 * Testcase for bgp_mp_list
197 */
198 struct peer test_mp_list_peer[] = {
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 { .local_as = 1, .as = 2 },
204 };
205 int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer);
206 struct attr test_mp_list_attr[4];
207 struct bgp_info test_mp_list_info[] = {
208 { .peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0] },
209 { .peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1] },
210 { .peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1] },
211 { .peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2] },
212 { .peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3] },
213 };
214 int test_mp_list_info_count =
215 sizeof (test_mp_list_info)/sizeof (struct bgp_info);
216
217 static int
218 setup_bgp_mp_list (testcase_t *t)
219 {
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")) == NULL)
226 return -1;
227 if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL)
228 return -1;
229 if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL)
230 return -1;
231 if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL)
232 return -1;
233 if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL)
234 return -1;
235
236 return 0;
237 }
238
239 static int
240 run_bgp_mp_list (testcase_t *t)
241 {
242 struct list mp_list;
243 struct listnode *mp_node;
244 struct bgp_info *info;
245 int i;
246 int test_result = TEST_PASSED;
247 bgp_mp_list_init (&mp_list);
248 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
249
250 bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
251 bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
252 bgp_mp_list_add (&mp_list, &test_mp_list_info[2]);
253 bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
254 bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
255
256 for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count;
257 i++, mp_node = listnextnode(mp_node))
258 {
259 info = listgetdata(mp_node);
260 EXPECT_TRUE (info == &test_mp_list_info[i], test_result);
261 }
262
263 bgp_mp_list_clear (&mp_list);
264 EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
265
266 return test_result;
267 }
268
269 static int
270 cleanup_bgp_mp_list (testcase_t *t)
271 {
272 int i;
273
274 for (i = 0; i < test_mp_list_peer_count; i++)
275 sockunion_free (test_mp_list_peer[i].su_remote);
276
277 return 0;
278 }
279
280 testcase_t test_bgp_mp_list = {
281 .desc = "Test bgp_mp_list",
282 .setup = setup_bgp_mp_list,
283 .run = run_bgp_mp_list,
284 .cleanup = cleanup_bgp_mp_list,
285 };
286
287 /*=========================================================
288 * Testcase for bgp_info_mpath_update
289 */
290
291 struct bgp_node test_rn;
292
293 static int
294 setup_bgp_info_mpath_update (testcase_t *t)
295 {
296 int i;
297 str2prefix ("42.1.1.0/24", &test_rn.p);
298 setup_bgp_mp_list (t);
299 for (i = 0; i < test_mp_list_info_count; i++)
300 bgp_info_add (&test_rn, &test_mp_list_info[i]);
301 return 0;
302 }
303
304 static int
305 run_bgp_info_mpath_update (testcase_t *t)
306 {
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;
342 }
343
344 static int
345 cleanup_bgp_info_mpath_update (testcase_t *t)
346 {
347 int i;
348
349 for (i = 0; i < test_mp_list_peer_count; i++)
350 sockunion_free (test_mp_list_peer[i].su_remote);
351
352 return 0;
353 }
354
355 testcase_t test_bgp_info_mpath_update = {
356 .desc = "Test bgp_info_mpath_update",
357 .setup = setup_bgp_info_mpath_update,
358 .run = run_bgp_info_mpath_update,
359 .cleanup = cleanup_bgp_info_mpath_update,
360 };
361
362 /*=========================================================
363 * Set up testcase vector
364 */
365 testcase_t *all_tests[] = {
366 &test_bgp_cfg_maximum_paths,
367 &test_bgp_mp_list,
368 &test_bgp_info_mpath_update,
369 };
370
371 int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *));
372
373 /*=========================================================
374 * Test Driver Functions
375 */
376 static int
377 global_test_init (void)
378 {
379 qobj_init ();
380 master = thread_master_create ();
381 zclient = zclient_new(master);
382 bgp_master_init (master);
383 vrf_init ();
384 bgp_option_set (BGP_OPT_NO_LISTEN);
385
386 if (fileno (stdout) >= 0)
387 tty = isatty (fileno (stdout));
388 return 0;
389 }
390
391 static int
392 global_test_cleanup (void)
393 {
394 if (zclient != NULL)
395 zclient_free (zclient);
396 thread_master_free (master);
397 return 0;
398 }
399
400 static void
401 display_result (testcase_t *test, int result)
402 {
403 if (tty)
404 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED);
405 else
406 printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED");
407 }
408
409 static int
410 setup_test (testcase_t *t)
411 {
412 int res = 0;
413 if (t->setup)
414 res = t->setup (t);
415 return res;
416 }
417
418 static int
419 cleanup_test (testcase_t *t)
420 {
421 int res = 0;
422 if (t->cleanup)
423 res = t->cleanup (t);
424 return res;
425 }
426
427 static void
428 run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count)
429 {
430 int test_index, result;
431 testcase_t *cur_test;
432
433 *pass_count = *fail_count = 0;
434
435 for (test_index = 0; test_index < num_tests; test_index++)
436 {
437 cur_test = tests[test_index];
438 if (!cur_test->desc)
439 {
440 printf ("error: test %d has no description!\n", test_index);
441 continue;
442 }
443 if (!cur_test->run)
444 {
445 printf ("error: test %s has no run function!\n", cur_test->desc);
446 continue;
447 }
448 if (setup_test (cur_test) != 0)
449 {
450 printf ("error: setup failed for test %s\n", cur_test->desc);
451 continue;
452 }
453 result = cur_test->run (cur_test);
454 if (result == TEST_PASSED)
455 *pass_count += 1;
456 else
457 *fail_count += 1;
458 display_result (cur_test, result);
459 if (cleanup_test (cur_test) != 0)
460 {
461 printf ("error: cleanup failed for test %s\n", cur_test->desc);
462 continue;
463 }
464 }
465 }
466
467 int
468 main (void)
469 {
470 int pass_count, fail_count;
471 time_t cur_time;
472
473 time (&cur_time);
474 printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
475 if (global_test_init () != 0)
476 {
477 printf("Global init failed. Terminating.\n");
478 exit(1);
479 }
480 run_tests (all_tests, all_tests_count, &pass_count, &fail_count);
481 global_test_cleanup ();
482 printf("Total pass/fail: %d/%d\n", pass_count, fail_count);
483 return fail_count;
484 }