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"
41 #include "bgpd/bgp_network.h"
43 #define VT100_RESET "\x1b[0m"
44 #define VT100_RED "\x1b[31m"
45 #define VT100_GREEN "\x1b[32m"
46 #define VT100_YELLOW "\x1b[33m"
47 #define OK VT100_GREEN "OK" VT100_RESET
48 #define FAILED VT100_RED "failed" VT100_RESET
51 #define TEST_FAILED -1
53 #define EXPECT_TRUE(expr, res) \
55 printf("Test failure in %s line %u: %s\n", __func__, __LINE__, \
57 (res) = TEST_FAILED; \
60 typedef struct testcase_t__ testcase_t
;
62 typedef int (*test_setup_func
)(testcase_t
*);
63 typedef int (*test_run_func
)(testcase_t
*);
64 typedef int (*test_cleanup_func
)(testcase_t
*);
71 test_setup_func setup
;
73 test_cleanup_func cleanup
;
76 /* need these to link in libbgp */
77 struct thread_master
*master
= NULL
;
78 extern struct zclient
*zclient
;
79 struct zebra_privs_t bgpd_privs
= {
87 /* Create fake bgp instance */
88 static struct bgp
*bgp_create_fake(as_t
*as
, const char *name
)
94 if ((bgp
= XCALLOC(MTYPE_BGP
, sizeof(struct bgp
))) == NULL
)
98 // bgp->peer_self = peer_new (bgp);
99 // bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static
102 bgp
->peer
= list_new();
103 // bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
105 bgp
->group
= list_new();
106 // bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp;
109 FOREACH_AFI_SAFI (afi
, safi
) {
110 bgp
->route
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
111 bgp
->aggregate
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
112 bgp
->rib
[afi
][safi
] = bgp_table_init(bgp
, afi
, safi
);
113 bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
= MULTIPATH_NUM
;
114 bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
= MULTIPATH_NUM
;
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
;
127 bgp
->name
= strdup(name
);
132 /*=========================================================
133 * Testcase for maximum-paths configuration
135 static int setup_bgp_cfg_maximum_paths(testcase_t
*t
)
138 t
->tmp_data
= bgp_create_fake(&asn
, NULL
);
144 static int run_bgp_cfg_maximum_paths(testcase_t
*t
)
150 int test_result
= TEST_PASSED
;
153 FOREACH_AFI_SAFI (afi
, safi
) {
154 /* test bgp_maximum_paths_set */
155 api_result
= bgp_maximum_paths_set(bgp
, afi
, safi
,
156 BGP_PEER_EBGP
, 10, 0);
157 EXPECT_TRUE(api_result
== 0, test_result
);
158 api_result
= bgp_maximum_paths_set(bgp
, afi
, safi
,
159 BGP_PEER_IBGP
, 10, 0);
160 EXPECT_TRUE(api_result
== 0, test_result
);
161 EXPECT_TRUE(bgp
->maxpaths
[afi
][safi
].maxpaths_ebgp
== 10,
163 EXPECT_TRUE(bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
== 10,
166 /* test bgp_maximum_paths_unset */
168 bgp_maximum_paths_unset(bgp
, afi
, safi
, BGP_PEER_EBGP
);
169 EXPECT_TRUE(api_result
== 0, test_result
);
171 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
176 EXPECT_TRUE((bgp
->maxpaths
[afi
][safi
].maxpaths_ibgp
184 static int cleanup_bgp_cfg_maximum_paths(testcase_t
*t
)
186 return bgp_delete((struct bgp
*)t
->tmp_data
);
189 testcase_t test_bgp_cfg_maximum_paths
= {
190 .desc
= "Test bgp maximum-paths config",
191 .setup
= setup_bgp_cfg_maximum_paths
,
192 .run
= run_bgp_cfg_maximum_paths
,
193 .cleanup
= cleanup_bgp_cfg_maximum_paths
,
196 /*=========================================================
197 * Testcase for bgp_mp_list
199 struct peer test_mp_list_peer
[] = {
200 {.local_as
= 1, .as
= 2}, {.local_as
= 1, .as
= 2},
201 {.local_as
= 1, .as
= 2}, {.local_as
= 1, .as
= 2},
202 {.local_as
= 1, .as
= 2},
204 int test_mp_list_peer_count
= array_size(test_mp_list_peer
);
205 struct attr test_mp_list_attr
[4];
206 struct bgp_path_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]},
213 int test_mp_list_info_count
= array_size(test_mp_list_info
);
215 static int setup_bgp_mp_list(testcase_t
*t
)
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;
222 if ((test_mp_list_peer
[0].su_remote
= sockunion_str2su("1.1.1.1"))
225 if ((test_mp_list_peer
[1].su_remote
= sockunion_str2su("2.2.2.2"))
228 if ((test_mp_list_peer
[2].su_remote
= sockunion_str2su("3.3.3.3"))
231 if ((test_mp_list_peer
[3].su_remote
= sockunion_str2su("4.4.4.4"))
234 if ((test_mp_list_peer
[4].su_remote
= sockunion_str2su("5.5.5.5"))
241 static int run_bgp_mp_list(testcase_t
*t
)
244 struct listnode
*mp_node
;
245 struct bgp_path_info
*info
;
247 int test_result
= TEST_PASSED
;
248 bgp_mp_list_init(&mp_list
);
249 EXPECT_TRUE(listcount(&mp_list
) == 0, test_result
);
251 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[1]);
252 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[4]);
253 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[2]);
254 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[3]);
255 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[0]);
257 for (i
= 0, mp_node
= mp_list
.head
; i
< test_mp_list_info_count
;
258 i
++, mp_node
= listnextnode(mp_node
)) {
259 info
= listgetdata(mp_node
);
260 EXPECT_TRUE(info
== &test_mp_list_info
[i
], test_result
);
263 bgp_mp_list_clear(&mp_list
);
264 EXPECT_TRUE(listcount(&mp_list
) == 0, test_result
);
269 static int cleanup_bgp_mp_list(testcase_t
*t
)
273 for (i
= 0; i
< test_mp_list_peer_count
; i
++)
274 sockunion_free(test_mp_list_peer
[i
].su_remote
);
279 testcase_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
,
286 /*=========================================================
287 * Testcase for bgp_path_info_mpath_update
290 struct bgp_node test_rn
;
292 static int setup_bgp_path_info_mpath_update(testcase_t
*t
)
296 struct bgp_table
*rt
;
297 struct route_node
*rt_node
;
300 t
->tmp_data
= bgp_create_fake(&asn
, NULL
);
305 rt
= bgp
->rib
[AFI_IP
][SAFI_UNICAST
];
310 str2prefix("42.1.1.0/24", &test_rn
.p
);
311 rt_node
= bgp_dest_to_rnode(&test_rn
);
312 memcpy((struct route_table
*)&rt_node
->table
, &rt
->route_table
,
313 sizeof(struct route_table
));
314 setup_bgp_mp_list(t
);
315 for (i
= 0; i
< test_mp_list_info_count
; i
++)
316 bgp_path_info_add(&test_rn
, &test_mp_list_info
[i
]);
320 static int run_bgp_path_info_mpath_update(testcase_t
*t
)
322 struct bgp_path_info
*new_best
, *old_best
, *mpath
;
324 struct bgp_maxpaths_cfg mp_cfg
= {3, 3};
325 int test_result
= TEST_PASSED
;
326 bgp_mp_list_init(&mp_list
);
327 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[4]);
328 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[3]);
329 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[0]);
330 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[1]);
331 new_best
= &test_mp_list_info
[3];
333 bgp_path_info_mpath_update(&test_rn
, new_best
, old_best
, &mp_list
,
335 bgp_mp_list_clear(&mp_list
);
336 EXPECT_TRUE(bgp_path_info_mpath_count(new_best
) == 2, test_result
);
337 mpath
= bgp_path_info_mpath_first(new_best
);
338 EXPECT_TRUE(mpath
== &test_mp_list_info
[0], test_result
);
339 EXPECT_TRUE(CHECK_FLAG(mpath
->flags
, BGP_PATH_MULTIPATH
), test_result
);
340 mpath
= bgp_path_info_mpath_next(mpath
);
341 EXPECT_TRUE(mpath
== &test_mp_list_info
[1], test_result
);
342 EXPECT_TRUE(CHECK_FLAG(mpath
->flags
, BGP_PATH_MULTIPATH
), test_result
);
344 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[0]);
345 bgp_mp_list_add(&mp_list
, &test_mp_list_info
[1]);
346 new_best
= &test_mp_list_info
[0];
347 old_best
= &test_mp_list_info
[3];
348 bgp_path_info_mpath_update(&test_rn
, new_best
, old_best
, &mp_list
,
350 bgp_mp_list_clear(&mp_list
);
351 EXPECT_TRUE(bgp_path_info_mpath_count(new_best
) == 1, test_result
);
352 mpath
= bgp_path_info_mpath_first(new_best
);
353 EXPECT_TRUE(mpath
== &test_mp_list_info
[1], test_result
);
354 EXPECT_TRUE(CHECK_FLAG(mpath
->flags
, BGP_PATH_MULTIPATH
), test_result
);
355 EXPECT_TRUE(!CHECK_FLAG(test_mp_list_info
[0].flags
, BGP_PATH_MULTIPATH
),
361 static int cleanup_bgp_path_info_mpath_update(testcase_t
*t
)
365 for (i
= 0; i
< test_mp_list_peer_count
; i
++)
366 sockunion_free(test_mp_list_peer
[i
].su_remote
);
368 return bgp_delete((struct bgp
*)t
->tmp_data
);
371 testcase_t test_bgp_path_info_mpath_update
= {
372 .desc
= "Test bgp_path_info_mpath_update",
373 .setup
= setup_bgp_path_info_mpath_update
,
374 .run
= run_bgp_path_info_mpath_update
,
375 .cleanup
= cleanup_bgp_path_info_mpath_update
,
378 /*=========================================================
379 * Set up testcase vector
381 testcase_t
*all_tests
[] = {
382 &test_bgp_cfg_maximum_paths
, &test_bgp_mp_list
,
383 &test_bgp_path_info_mpath_update
,
386 int all_tests_count
= array_size(all_tests
);
388 /*=========================================================
389 * Test Driver Functions
391 static int global_test_init(void)
394 master
= thread_master_create(NULL
);
395 zclient
= zclient_new(master
, &zclient_options_default
, NULL
, 0);
396 bgp_master_init(master
, BGP_SOCKET_SNDBUF_SIZE
, list_new());
397 vrf_init(NULL
, NULL
, NULL
, NULL
, NULL
);
398 bgp_option_set(BGP_OPT_NO_LISTEN
);
400 if (fileno(stdout
) >= 0)
401 tty
= isatty(fileno(stdout
));
405 static int global_test_cleanup(void)
408 zclient_free(zclient
);
409 thread_master_free(master
);
413 static void display_result(testcase_t
*test
, int result
)
416 printf("%s: %s\n", test
->desc
,
417 result
== TEST_PASSED
? OK
: FAILED
);
419 printf("%s: %s\n", test
->desc
,
420 result
== TEST_PASSED
? "OK" : "FAILED");
423 static int setup_test(testcase_t
*t
)
431 static int cleanup_test(testcase_t
*t
)
439 static void run_tests(testcase_t
*tests
[], int num_tests
, int *pass_count
,
442 int test_index
, result
;
443 testcase_t
*cur_test
;
445 *pass_count
= *fail_count
= 0;
447 for (test_index
= 0; test_index
< num_tests
; test_index
++) {
448 cur_test
= tests
[test_index
];
449 if (!cur_test
->desc
) {
450 printf("error: test %d has no description!\n",
454 if (!cur_test
->run
) {
455 printf("error: test %s has no run function!\n",
459 if (setup_test(cur_test
) != 0) {
460 printf("error: setup failed for test %s\n",
464 result
= cur_test
->run(cur_test
);
465 if (result
== TEST_PASSED
)
469 display_result(cur_test
, result
);
470 if (cleanup_test(cur_test
) != 0) {
471 printf("error: cleanup failed for test %s\n",
480 int pass_count
, fail_count
;
484 printf("BGP Multipath Tests Run at %s", ctime(&cur_time
));
485 if (global_test_init() != 0) {
486 printf("Global init failed. Terminating.\n");
489 run_tests(all_tests
, all_tests_count
, &pass_count
, &fail_count
);
490 global_test_cleanup();
491 printf("Total pass/fail: %d/%d\n", pass_count
, fail_count
);