]> git.proxmox.com Git - mirror_frr.git/blob - tests/bgpd/test_mpath.c
Merge pull request #837 from qlyoung/fix-eigrp-routemap-cli
[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 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
20 */
21
22 #include <zebra.h>
23
24 #include "qobj.h"
25 #include "vty.h"
26 #include "stream.h"
27 #include "privs.h"
28 #include "linklist.h"
29 #include "memory.h"
30 #include "zclient.h"
31 #include "queue.h"
32 #include "filter.h"
33
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
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 printf("Test failure in %s line %u: %s\n", __FUNCTION__, \
54 __LINE__, #expr); \
55 (res) = TEST_FAILED; \
56 }
57
58 typedef struct testcase_t__ testcase_t;
59
60 typedef int (*test_setup_func)(testcase_t *);
61 typedef int (*test_run_func)(testcase_t *);
62 typedef int (*test_cleanup_func)(testcase_t *);
63
64 struct testcase_t__ {
65 const char *desc;
66 void *test_data;
67 void *verify_data;
68 void *tmp_data;
69 test_setup_func setup;
70 test_run_func run;
71 test_cleanup_func cleanup;
72 };
73
74 /* need these to link in libbgp */
75 struct thread_master *master = NULL;
76 struct zclient *zclient;
77 struct zebra_privs_t bgpd_privs = {
78 .user = NULL,
79 .group = NULL,
80 .vty_group = NULL,
81 };
82
83 static int tty = 0;
84
85 /* Create fake bgp instance */
86 static struct bgp *bgp_create_fake(as_t *as, const char *name)
87 {
88 struct bgp *bgp;
89 afi_t afi;
90 safi_t safi;
91
92 if ((bgp = XCALLOC(MTYPE_BGP, sizeof(struct bgp))) == NULL)
93 return NULL;
94
95 bgp_lock(bgp);
96 // bgp->peer_self = peer_new (bgp);
97 // bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static
98 // 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 for (afi = AFI_IP; afi < AFI_MAX; afi++)
107 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
108 bgp->route[afi][safi] = bgp_table_init(afi, safi);
109 bgp->aggregate[afi][safi] = bgp_table_init(afi, safi);
110 bgp->rib[afi][safi] = bgp_table_init(afi, safi);
111 bgp->maxpaths[afi][safi].maxpaths_ebgp = MULTIPATH_NUM;
112 bgp->maxpaths[afi][safi].maxpaths_ibgp = MULTIPATH_NUM;
113 }
114
115 bgp_scan_init(bgp);
116 bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
117 bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
118 bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
119 bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
120 bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
121
122 bgp->as = *as;
123
124 if (name)
125 bgp->name = strdup(name);
126
127 return bgp;
128 }
129
130 /*=========================================================
131 * Testcase for maximum-paths configuration
132 */
133 static int setup_bgp_cfg_maximum_paths(testcase_t *t)
134 {
135 as_t asn = 1;
136 t->tmp_data = bgp_create_fake(&asn, NULL);
137 if (!t->tmp_data)
138 return -1;
139 return 0;
140 }
141
142 static int run_bgp_cfg_maximum_paths(testcase_t *t)
143 {
144 afi_t afi;
145 safi_t safi;
146 struct bgp *bgp;
147 int api_result;
148 int test_result = TEST_PASSED;
149
150 bgp = t->tmp_data;
151 for (afi = AFI_IP; afi < AFI_MAX; afi++)
152 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
153 /* test bgp_maximum_paths_set */
154 api_result = bgp_maximum_paths_set(
155 bgp, afi, safi, BGP_PEER_EBGP, 10, 0);
156 EXPECT_TRUE(api_result == 0, test_result);
157 api_result = bgp_maximum_paths_set(
158 bgp, afi, safi, BGP_PEER_IBGP, 10, 0);
159 EXPECT_TRUE(api_result == 0, test_result);
160 EXPECT_TRUE(bgp->maxpaths[afi][safi].maxpaths_ebgp
161 == 10,
162 test_result);
163 EXPECT_TRUE(bgp->maxpaths[afi][safi].maxpaths_ibgp
164 == 10,
165 test_result);
166
167 /* test bgp_maximum_paths_unset */
168 api_result = bgp_maximum_paths_unset(bgp, afi, safi,
169 BGP_PEER_EBGP);
170 EXPECT_TRUE(api_result == 0, test_result);
171 api_result = bgp_maximum_paths_unset(bgp, afi, safi,
172 BGP_PEER_IBGP);
173 EXPECT_TRUE(api_result == 0, test_result);
174 EXPECT_TRUE((bgp->maxpaths[afi][safi].maxpaths_ebgp
175 == MULTIPATH_NUM),
176 test_result);
177 EXPECT_TRUE((bgp->maxpaths[afi][safi].maxpaths_ibgp
178 == MULTIPATH_NUM),
179 test_result);
180 }
181
182 return test_result;
183 }
184
185 static int cleanup_bgp_cfg_maximum_paths(testcase_t *t)
186 {
187 return bgp_delete((struct bgp *)t->tmp_data);
188 }
189
190 testcase_t test_bgp_cfg_maximum_paths = {
191 .desc = "Test bgp maximum-paths config",
192 .setup = setup_bgp_cfg_maximum_paths,
193 .run = run_bgp_cfg_maximum_paths,
194 .cleanup = cleanup_bgp_cfg_maximum_paths,
195 };
196
197 /*=========================================================
198 * Testcase for bgp_mp_list
199 */
200 struct peer test_mp_list_peer[] = {
201 {.local_as = 1, .as = 2}, {.local_as = 1, .as = 2},
202 {.local_as = 1, .as = 2}, {.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 setup_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"))
225 == NULL)
226 return -1;
227 if ((test_mp_list_peer[1].su_remote = sockunion_str2su("2.2.2.2"))
228 == NULL)
229 return -1;
230 if ((test_mp_list_peer[2].su_remote = sockunion_str2su("3.3.3.3"))
231 == NULL)
232 return -1;
233 if ((test_mp_list_peer[3].su_remote = sockunion_str2su("4.4.4.4"))
234 == NULL)
235 return -1;
236 if ((test_mp_list_peer[4].su_remote = sockunion_str2su("5.5.5.5"))
237 == NULL)
238 return -1;
239
240 return 0;
241 }
242
243 static int run_bgp_mp_list(testcase_t *t)
244 {
245 struct list mp_list;
246 struct listnode *mp_node;
247 struct bgp_info *info;
248 int i;
249 int test_result = TEST_PASSED;
250 bgp_mp_list_init(&mp_list);
251 EXPECT_TRUE(listcount(&mp_list) == 0, test_result);
252
253 bgp_mp_list_add(&mp_list, &test_mp_list_info[1]);
254 bgp_mp_list_add(&mp_list, &test_mp_list_info[4]);
255 bgp_mp_list_add(&mp_list, &test_mp_list_info[2]);
256 bgp_mp_list_add(&mp_list, &test_mp_list_info[3]);
257 bgp_mp_list_add(&mp_list, &test_mp_list_info[0]);
258
259 for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count;
260 i++, mp_node = listnextnode(mp_node)) {
261 info = listgetdata(mp_node);
262 EXPECT_TRUE(info == &test_mp_list_info[i], test_result);
263 }
264
265 bgp_mp_list_clear(&mp_list);
266 EXPECT_TRUE(listcount(&mp_list) == 0, test_result);
267
268 return test_result;
269 }
270
271 static int cleanup_bgp_mp_list(testcase_t *t)
272 {
273 int i;
274
275 for (i = 0; i < test_mp_list_peer_count; i++)
276 sockunion_free(test_mp_list_peer[i].su_remote);
277
278 return 0;
279 }
280
281 testcase_t test_bgp_mp_list = {
282 .desc = "Test bgp_mp_list",
283 .setup = setup_bgp_mp_list,
284 .run = run_bgp_mp_list,
285 .cleanup = cleanup_bgp_mp_list,
286 };
287
288 /*=========================================================
289 * Testcase for bgp_info_mpath_update
290 */
291
292 struct bgp_node test_rn;
293
294 static int 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 run_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
343 static int cleanup_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
353 testcase_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
360 /*=========================================================
361 * Set up testcase vector
362 */
363 testcase_t *all_tests[] = {
364 &test_bgp_cfg_maximum_paths, &test_bgp_mp_list,
365 &test_bgp_info_mpath_update,
366 };
367
368 int all_tests_count = (sizeof(all_tests) / sizeof(testcase_t *));
369
370 /*=========================================================
371 * Test Driver Functions
372 */
373 static int global_test_init(void)
374 {
375 qobj_init();
376 master = thread_master_create(NULL);
377 zclient = zclient_new(master);
378 bgp_master_init(master);
379 vrf_init(NULL, NULL, NULL, NULL);
380 bgp_option_set(BGP_OPT_NO_LISTEN);
381
382 if (fileno(stdout) >= 0)
383 tty = isatty(fileno(stdout));
384 return 0;
385 }
386
387 static int global_test_cleanup(void)
388 {
389 if (zclient != NULL)
390 zclient_free(zclient);
391 thread_master_free(master);
392 return 0;
393 }
394
395 static void display_result(testcase_t *test, int result)
396 {
397 if (tty)
398 printf("%s: %s\n", test->desc,
399 result == TEST_PASSED ? OK : FAILED);
400 else
401 printf("%s: %s\n", test->desc,
402 result == TEST_PASSED ? "OK" : "FAILED");
403 }
404
405 static int setup_test(testcase_t *t)
406 {
407 int res = 0;
408 if (t->setup)
409 res = t->setup(t);
410 return res;
411 }
412
413 static int cleanup_test(testcase_t *t)
414 {
415 int res = 0;
416 if (t->cleanup)
417 res = t->cleanup(t);
418 return res;
419 }
420
421 static void run_tests(testcase_t *tests[], int num_tests, int *pass_count,
422 int *fail_count)
423 {
424 int test_index, result;
425 testcase_t *cur_test;
426
427 *pass_count = *fail_count = 0;
428
429 for (test_index = 0; test_index < num_tests; test_index++) {
430 cur_test = tests[test_index];
431 if (!cur_test->desc) {
432 printf("error: test %d has no description!\n",
433 test_index);
434 continue;
435 }
436 if (!cur_test->run) {
437 printf("error: test %s has no run function!\n",
438 cur_test->desc);
439 continue;
440 }
441 if (setup_test(cur_test) != 0) {
442 printf("error: setup failed for test %s\n",
443 cur_test->desc);
444 continue;
445 }
446 result = cur_test->run(cur_test);
447 if (result == TEST_PASSED)
448 *pass_count += 1;
449 else
450 *fail_count += 1;
451 display_result(cur_test, result);
452 if (cleanup_test(cur_test) != 0) {
453 printf("error: cleanup failed for test %s\n",
454 cur_test->desc);
455 continue;
456 }
457 }
458 }
459
460 int main(void)
461 {
462 int pass_count, fail_count;
463 time_t cur_time;
464
465 time(&cur_time);
466 printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
467 if (global_test_init() != 0) {
468 printf("Global init failed. Terminating.\n");
469 exit(1);
470 }
471 run_tests(all_tests, all_tests_count, &pass_count, &fail_count);
472 global_test_cleanup();
473 printf("Total pass/fail: %d/%d\n", pass_count, fail_count);
474 return fail_count;
475 }