2 * BGP Multipath Unit Test
3 * Copyright (C) 2010 Google Inc.
5 * This file is part of Quagga
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
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_table.h"
36 #include "bgpd/bgp_route.h"
37 #include "bgpd/bgp_attr.h"
38 #include "bgpd/bgp_nexthop.h"
39 #include "bgpd/bgp_mpath.h"
40 #include "bgpd/bgp_evpn.h"
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
50 #define TEST_FAILED -1
52 #define EXPECT_TRUE(expr, res) \
54 printf("Test failure in %s line %u: %s\n", __FUNCTION__, \
56 (res) = TEST_FAILED; \
59 typedef struct testcase_t__ testcase_t
;
61 typedef int (*test_setup_func
)(testcase_t
*);
62 typedef int (*test_run_func
)(testcase_t
*);
63 typedef int (*test_cleanup_func
)(testcase_t
*);
70 test_setup_func setup
;
72 test_cleanup_func cleanup
;
75 /* need these to link in libbgp */
76 struct thread_master
*master
= NULL
;
77 struct zclient
*zclient
;
78 struct zebra_privs_t bgpd_privs
= {
86 /* Create fake bgp instance */
87 static struct bgp
*bgp_create_fake(as_t
*as
, const char *name
)
93 if ((bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
))) == NULL
)
97 // bgp->peer_self = peer_new (bgp);
98 // bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static
101 bgp
->peer
= list_new();
102 // bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
104 bgp
->group
= list_new();
105 // bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp;
108 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
109 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
110 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
111 bgp
->aggregate
[afi
][safi
] = bgp_table_init(
113 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
114 bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
= MULTIPATH_NUM
;
115 bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
= MULTIPATH_NUM
;
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
;
128 bgp
->name
= strdup(name
);
133 /*=========================================================
134 * Testcase for maximum-paths configuration
136 static int setup_bgp_cfg_maximum_paths(testcase_t
*t
)
139 t
->tmp_data
= bgp_create_fake(&asn
, NULL
);
145 static int run_bgp_cfg_maximum_paths(testcase_t
*t
)
151 int test_result
= TEST_PASSED
;
154 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
155 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++) {
156 /* test bgp_maximum_paths_set */
157 api_result
= bgp_maximum_paths_set(
158 bgp
, afi
, safi
, BGP_PEER_EBGP
, 10, 0);
159 EXPECT_TRUE(api_result
== 0, test_result
);
160 api_result
= bgp_maximum_paths_set(
161 bgp
, afi
, safi
, BGP_PEER_IBGP
, 10, 0);
162 EXPECT_TRUE(api_result
== 0, test_result
);
163 EXPECT_TRUE(bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
166 EXPECT_TRUE(bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
170 /* test bgp_maximum_paths_unset */
171 api_result
= bgp_maximum_paths_unset(bgp
, afi
, safi
,
173 EXPECT_TRUE(api_result
== 0, test_result
);
174 api_result
= bgp_maximum_paths_unset(bgp
, afi
, safi
,
176 EXPECT_TRUE(api_result
== 0, test_result
);
177 EXPECT_TRUE((bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
180 EXPECT_TRUE((bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
188 static int cleanup_bgp_cfg_maximum_paths(testcase_t
*t
)
190 return bgp_delete((struct bgp
*)t
->tmp_data
);
193 testcase_t test_bgp_cfg_maximum_paths
= {
194 .desc
= "Test bgp maximum-paths config",
195 .setup
= setup_bgp_cfg_maximum_paths
,
196 .run
= run_bgp_cfg_maximum_paths
,
197 .cleanup
= cleanup_bgp_cfg_maximum_paths
,
200 /*=========================================================
201 * Testcase for bgp_mp_list
203 struct peer test_mp_list_peer
[] = {
204 {.local_as
= 1, .as
= 2}, {.local_as
= 1, .as
= 2},
205 {.local_as
= 1, .as
= 2}, {.local_as
= 1, .as
= 2},
206 {.local_as
= 1, .as
= 2},
208 int test_mp_list_peer_count
= sizeof(test_mp_list_peer
) / sizeof(struct peer
);
209 struct attr test_mp_list_attr
[4];
210 struct bgp_path_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]},
217 int test_mp_list_info_count
=
218 sizeof(test_mp_list_info
) / sizeof(struct bgp_path_info
);
220 static int setup_bgp_mp_list(testcase_t
*t
)
222 test_mp_list_attr
[0].nexthop
.s_addr
= 0x01010101;
223 test_mp_list_attr
[1].nexthop
.s_addr
= 0x02020202;
224 test_mp_list_attr
[2].nexthop
.s_addr
= 0x03030303;
225 test_mp_list_attr
[3].nexthop
.s_addr
= 0x04040404;
227 if ((test_mp_list_peer
[0].su_remote
= sockunion_str2su("1.1.1.1"))
230 if ((test_mp_list_peer
[1].su_remote
= sockunion_str2su("2.2.2.2"))
233 if ((test_mp_list_peer
[2].su_remote
= sockunion_str2su("3.3.3.3"))
236 if ((test_mp_list_peer
[3].su_remote
= sockunion_str2su("4.4.4.4"))
239 if ((test_mp_list_peer
[4].su_remote
= sockunion_str2su("5.5.5.5"))
246 static int run_bgp_mp_list(testcase_t
*t
)
249 struct listnode
*mp_node
;
250 struct bgp_path_info
*info
;
252 int test_result
= TEST_PASSED
;
253 bgp_mp_list_init(&mp_list
);
254 EXPECT_TRUE(listcount(&mp_list
) == 0, test_result
);
256 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[1]);
257 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[4]);
258 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[2]);
259 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[3]);
260 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[0]);
262 for (i
= 0, mp_node
= mp_list
.head
; i
< test_mp_list_info_count
;
263 i
++, mp_node
= listnextnode(mp_node
)) {
264 info
= listgetdata(mp_node
);
265 EXPECT_TRUE(info
== &test_mp_list_info
[i
], test_result
);
268 bgp_mp_list_clear(&mp_list
);
269 EXPECT_TRUE(listcount(&mp_list
) == 0, test_result
);
274 static int cleanup_bgp_mp_list(testcase_t
*t
)
278 for (i
= 0; i
< test_mp_list_peer_count
; i
++)
279 sockunion_free(test_mp_list_peer
[i
].su_remote
);
284 testcase_t test_bgp_mp_list
= {
285 .desc
= "Test bgp_mp_list",
286 .setup
= setup_bgp_mp_list
,
287 .run
= run_bgp_mp_list
,
288 .cleanup
= cleanup_bgp_mp_list
,
291 /*=========================================================
292 * Testcase for bgp_path_info_mpath_update
295 struct bgp_node test_rn
;
297 static int setup_bgp_path_info_mpath_update(testcase_t
*t
)
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_path_info_add(&test_rn
, &test_mp_list_info
[i
]);
307 static int run_bgp_path_info_mpath_update(testcase_t
*t
)
309 struct bgp_path_info
*new_best
, *old_best
, *mpath
;
311 struct bgp_maxpaths_cfg mp_cfg
= {3, 3};
312 int test_result
= TEST_PASSED
;
313 bgp_mp_list_init(&mp_list
);
314 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[4]);
315 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[3]);
316 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[0]);
317 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[1]);
318 new_best
= &test_mp_list_info
[3];
320 bgp_path_info_mpath_update(&test_rn
, new_best
, old_best
, &mp_list
,
322 bgp_mp_list_clear(&mp_list
);
323 EXPECT_TRUE(bgp_path_info_mpath_count(new_best
) == 2, test_result
);
324 mpath
= bgp_path_info_mpath_first(new_best
);
325 EXPECT_TRUE(mpath
== &test_mp_list_info
[0], test_result
);
326 EXPECT_TRUE(CHECK_FLAG(mpath
->flags
, BGP_PATH_MULTIPATH
), test_result
);
327 mpath
= bgp_path_info_mpath_next(mpath
);
328 EXPECT_TRUE(mpath
== &test_mp_list_info
[1], test_result
);
329 EXPECT_TRUE(CHECK_FLAG(mpath
->flags
, BGP_PATH_MULTIPATH
), test_result
);
331 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[0]);
332 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[1]);
333 new_best
= &test_mp_list_info
[0];
334 old_best
= &test_mp_list_info
[3];
335 bgp_path_info_mpath_update(&test_rn
, new_best
, old_best
, &mp_list
,
337 bgp_mp_list_clear(&mp_list
);
338 EXPECT_TRUE(bgp_path_info_mpath_count(new_best
) == 1, test_result
);
339 mpath
= bgp_path_info_mpath_first(new_best
);
340 EXPECT_TRUE(mpath
== &test_mp_list_info
[1], test_result
);
341 EXPECT_TRUE(CHECK_FLAG(mpath
->flags
, BGP_PATH_MULTIPATH
), test_result
);
342 EXPECT_TRUE(!CHECK_FLAG(test_mp_list_info
[0].flags
, BGP_PATH_MULTIPATH
),
348 static int cleanup_bgp_path_info_mpath_update(testcase_t
*t
)
352 for (i
= 0; i
< test_mp_list_peer_count
; i
++)
353 sockunion_free(test_mp_list_peer
[i
].su_remote
);
358 testcase_t test_bgp_path_info_mpath_update
= {
359 .desc
= "Test bgp_path_info_mpath_update",
360 .setup
= setup_bgp_path_info_mpath_update
,
361 .run
= run_bgp_path_info_mpath_update
,
362 .cleanup
= cleanup_bgp_path_info_mpath_update
,
365 /*=========================================================
366 * Set up testcase vector
368 testcase_t
*all_tests
[] = {
369 &test_bgp_cfg_maximum_paths
, &test_bgp_mp_list
,
370 &test_bgp_path_info_mpath_update
,
373 int all_tests_count
= (sizeof(all_tests
) / sizeof(testcase_t
*));
375 /*=========================================================
376 * Test Driver Functions
378 static int global_test_init(void)
381 master
= thread_master_create(NULL
);
382 zclient
= zclient_new_notify(master
, &zclient_options_default
);
383 bgp_master_init(master
);
384 vrf_init(NULL
, NULL
, NULL
, NULL
, NULL
);
385 bgp_option_set(BGP_OPT_NO_LISTEN
);
387 if (fileno(stdout
) >= 0)
388 tty
= isatty(fileno(stdout
));
392 static int global_test_cleanup(void)
395 zclient_free(zclient
);
396 thread_master_free(master
);
400 static void display_result(testcase_t
*test
, int result
)
403 printf("%s: %s\n", test
->desc
,
404 result
== TEST_PASSED
? OK
: FAILED
);
406 printf("%s: %s\n", test
->desc
,
407 result
== TEST_PASSED
? "OK" : "FAILED");
410 static int setup_test(testcase_t
*t
)
418 static int cleanup_test(testcase_t
*t
)
426 static void run_tests(testcase_t
*tests
[], int num_tests
, int *pass_count
,
429 int test_index
, result
;
430 testcase_t
*cur_test
;
432 *pass_count
= *fail_count
= 0;
434 for (test_index
= 0; test_index
< num_tests
; test_index
++) {
435 cur_test
= tests
[test_index
];
436 if (!cur_test
->desc
) {
437 printf("error: test %d has no description!\n",
441 if (!cur_test
->run
) {
442 printf("error: test %s has no run function!\n",
446 if (setup_test(cur_test
) != 0) {
447 printf("error: setup failed for test %s\n",
451 result
= cur_test
->run(cur_test
);
452 if (result
== TEST_PASSED
)
456 display_result(cur_test
, result
);
457 if (cleanup_test(cur_test
) != 0) {
458 printf("error: cleanup failed for test %s\n",
467 int pass_count
, fail_count
;
471 printf("BGP Multipath Tests Run at %s", ctime(&cur_time
));
472 if (global_test_init() != 0) {
473 printf("Global init failed. Terminating.\n");
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
);