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