bgp_mpath_test.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. #include <zebra.h>
  24. #include "vty.h"
  25. #include "stream.h"
  26. #include "privs.h"
  27. #include "linklist.h"
  28. #include "memory.h"
  29. #include "zclient.h"
  30. #include "bgpd/bgpd.h"
  31. #include "bgpd/bgp_table.h"
  32. #include "bgpd/bgp_route.h"
  33. #include "bgpd/bgp_attr.h"
  34. #include "bgpd/bgp_mpath.h"
  35. #define VT100_RESET "\x1b[0m"
  36. #define VT100_RED "\x1b[31m"
  37. #define VT100_GREEN "\x1b[32m"
  38. #define VT100_YELLOW "\x1b[33m"
  39. #define OK VT100_GREEN "OK" VT100_RESET
  40. #define FAILED VT100_RED "failed" VT100_RESET
  41. #define TEST_PASSED 0
  42. #define TEST_FAILED -1
  43. #define EXPECT_TRUE(expr, res) \
  44. if (!(expr)) \
  45. { \
  46. printf ("Test failure in %s line %u: %s\n", \
  47. __FUNCTION__, __LINE__, #expr); \
  48. (res) = TEST_FAILED; \
  49. }
  50. typedef struct testcase_t__ testcase_t;
  51. typedef int (*test_setup_func)(testcase_t *);
  52. typedef int (*test_run_func)(testcase_t *);
  53. typedef int (*test_cleanup_func)(testcase_t *);
  54. struct testcase_t__ {
  55. const char *desc;
  56. void *test_data;
  57. void *verify_data;
  58. void *tmp_data;
  59. test_setup_func setup;
  60. test_run_func run;
  61. test_cleanup_func cleanup;
  62. };
  63. /* need these to link in libbgp */
  64. struct thread_master *master = NULL;
  65. struct zclient *zclient;
  66. struct zebra_privs_t bgpd_privs =
  67. {
  68. .user = NULL,
  69. .group = NULL,
  70. .vty_group = NULL,
  71. };
  72. static int tty = 0;
  73. /* Create fake bgp instance */
  74. static struct bgp *
  75. bgp_create_fake (as_t *as, const char *name)
  76. {
  77. struct bgp *bgp;
  78. afi_t afi;
  79. safi_t safi;
  80. if ( (bgp = XCALLOC (MTYPE_BGP, sizeof (struct bgp))) == NULL)
  81. return NULL;
  82. bgp_lock (bgp);
  83. //bgp->peer_self = peer_new (bgp);
  84. //bgp->peer_self->host = XSTRDUP (MTYPE_BGP_PEER_HOST, "Static announcement");
  85. bgp->peer = list_new ();
  86. //bgp->peer->cmp = (int (*)(void *, void *)) peer_cmp;
  87. bgp->group = list_new ();
  88. //bgp->group->cmp = (int (*)(void *, void *)) peer_group_cmp;
  89. bgp->rsclient = list_new ();
  90. //bgp->rsclient->cmp = (int (*)(void*, void*)) peer_cmp;
  91. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  92. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  93. {
  94. bgp->route[afi][safi] = bgp_table_init (afi, safi);
  95. bgp->aggregate[afi][safi] = bgp_table_init (afi, safi);
  96. bgp->rib[afi][safi] = bgp_table_init (afi, safi);
  97. bgp->maxpaths[afi][safi].maxpaths_ebgp = BGP_DEFAULT_MAXPATHS;
  98. bgp->maxpaths[afi][safi].maxpaths_ibgp = BGP_DEFAULT_MAXPATHS;
  99. }
  100. bgp->default_local_pref = BGP_DEFAULT_LOCAL_PREF;
  101. bgp->default_holdtime = BGP_DEFAULT_HOLDTIME;
  102. bgp->default_keepalive = BGP_DEFAULT_KEEPALIVE;
  103. bgp->restart_time = BGP_DEFAULT_RESTART_TIME;
  104. bgp->stalepath_time = BGP_DEFAULT_STALEPATH_TIME;
  105. bgp->as = *as;
  106. if (name)
  107. bgp->name = strdup (name);
  108. return bgp;
  109. }
  110. /*=========================================================
  111. * Testcase for maximum-paths configuration
  112. */
  113. static int
  114. setup_bgp_cfg_maximum_paths (testcase_t *t)
  115. {
  116. as_t asn = 1;
  117. t->tmp_data = bgp_create_fake (&asn, NULL);
  118. if (!t->tmp_data)
  119. return -1;
  120. return 0;
  121. }
  122. static int
  123. run_bgp_cfg_maximum_paths (testcase_t *t)
  124. {
  125. afi_t afi;
  126. safi_t safi;
  127. struct bgp *bgp;
  128. int api_result;
  129. int test_result = TEST_PASSED;
  130. bgp = t->tmp_data;
  131. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  132. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  133. {
  134. /* test bgp_maximum_paths_set */
  135. api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_EBGP, 10);
  136. EXPECT_TRUE (api_result == 0, test_result);
  137. api_result = bgp_maximum_paths_set (bgp, afi, safi, BGP_PEER_IBGP, 10);
  138. EXPECT_TRUE (api_result == 0, test_result);
  139. EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ebgp == 10, test_result);
  140. EXPECT_TRUE (bgp->maxpaths[afi][safi].maxpaths_ibgp == 10, test_result);
  141. /* test bgp_maximum_paths_unset */
  142. api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_EBGP);
  143. EXPECT_TRUE (api_result == 0, test_result);
  144. api_result = bgp_maximum_paths_unset (bgp, afi, safi, BGP_PEER_IBGP);
  145. EXPECT_TRUE (api_result == 0, test_result);
  146. EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ebgp ==
  147. BGP_DEFAULT_MAXPATHS), test_result);
  148. EXPECT_TRUE ((bgp->maxpaths[afi][safi].maxpaths_ibgp ==
  149. BGP_DEFAULT_MAXPATHS), test_result);
  150. }
  151. return test_result;
  152. }
  153. static int
  154. cleanup_bgp_cfg_maximum_paths (testcase_t *t)
  155. {
  156. return bgp_delete ((struct bgp *)t->tmp_data);
  157. }
  158. testcase_t test_bgp_cfg_maximum_paths = {
  159. .desc = "Test bgp maximum-paths config",
  160. .setup = setup_bgp_cfg_maximum_paths,
  161. .run = run_bgp_cfg_maximum_paths,
  162. .cleanup = cleanup_bgp_cfg_maximum_paths,
  163. };
  164. /*=========================================================
  165. * Testcase for bgp_mp_list
  166. */
  167. struct peer test_mp_list_peer[] = {
  168. { .local_as = 1, .as = 2 },
  169. { .local_as = 1, .as = 2 },
  170. { .local_as = 1, .as = 2 },
  171. { .local_as = 1, .as = 2 },
  172. { .local_as = 1, .as = 2 },
  173. };
  174. int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer);
  175. struct attr test_mp_list_attr[4];
  176. struct bgp_info test_mp_list_info[] = {
  177. { .peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0] },
  178. { .peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1] },
  179. { .peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1] },
  180. { .peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2] },
  181. { .peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3] },
  182. };
  183. int test_mp_list_info_count =
  184. sizeof (test_mp_list_info)/sizeof (struct bgp_info);
  185. static int
  186. setup_bgp_mp_list (testcase_t *t)
  187. {
  188. test_mp_list_attr[0].nexthop.s_addr = 0x01010101;
  189. test_mp_list_attr[1].nexthop.s_addr = 0x02020202;
  190. test_mp_list_attr[2].nexthop.s_addr = 0x03030303;
  191. test_mp_list_attr[3].nexthop.s_addr = 0x04040404;
  192. if ((test_mp_list_peer[0].su_remote = sockunion_str2su ("1.1.1.1")) == NULL)
  193. return -1;
  194. if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL)
  195. return -1;
  196. if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL)
  197. return -1;
  198. if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL)
  199. return -1;
  200. if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL)
  201. return -1;
  202. return 0;
  203. }
  204. static int
  205. run_bgp_mp_list (testcase_t *t)
  206. {
  207. struct list mp_list;
  208. struct listnode *mp_node;
  209. struct bgp_info *info;
  210. int i;
  211. int test_result = TEST_PASSED;
  212. bgp_mp_list_init (&mp_list);
  213. EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
  214. bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
  215. bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
  216. bgp_mp_list_add (&mp_list, &test_mp_list_info[2]);
  217. bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
  218. bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
  219. for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count;
  220. i++, mp_node = listnextnode(mp_node))
  221. {
  222. info = listgetdata(mp_node);
  223. EXPECT_TRUE (info == &test_mp_list_info[i], test_result);
  224. }
  225. bgp_mp_list_clear (&mp_list);
  226. EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
  227. return test_result;
  228. }
  229. static int
  230. cleanup_bgp_mp_list (testcase_t *t)
  231. {
  232. int i;
  233. for (i = 0; i < test_mp_list_peer_count; i++)
  234. sockunion_free (test_mp_list_peer[i].su_remote);
  235. return 0;
  236. }
  237. testcase_t test_bgp_mp_list = {
  238. .desc = "Test bgp_mp_list",
  239. .setup = setup_bgp_mp_list,
  240. .run = run_bgp_mp_list,
  241. .cleanup = cleanup_bgp_mp_list,
  242. };
  243. /*=========================================================
  244. * Testcase for bgp_info_mpath_update
  245. */
  246. struct bgp_node test_rn;
  247. static int
  248. setup_bgp_info_mpath_update (testcase_t *t)
  249. {
  250. int i;
  251. str2prefix ("42.1.1.0/24", &test_rn.p);
  252. setup_bgp_mp_list (t);
  253. for (i = 0; i < test_mp_list_info_count; i++)
  254. bgp_info_add (&test_rn, &test_mp_list_info[i]);
  255. return 0;
  256. }
  257. static int
  258. run_bgp_info_mpath_update (testcase_t *t)
  259. {
  260. struct bgp_info *new_best, *old_best, *mpath;
  261. struct list mp_list;
  262. struct bgp_maxpaths_cfg mp_cfg = { 3, 3 };
  263. int test_result = TEST_PASSED;
  264. bgp_mp_list_init (&mp_list);
  265. bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
  266. bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
  267. bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
  268. bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
  269. new_best = &test_mp_list_info[3];
  270. old_best = NULL;
  271. bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, &mp_cfg);
  272. bgp_mp_list_clear (&mp_list);
  273. EXPECT_TRUE (bgp_info_mpath_count (new_best) == 2, test_result);
  274. mpath = bgp_info_mpath_first (new_best);
  275. EXPECT_TRUE (mpath == &test_mp_list_info[0], test_result);
  276. EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
  277. mpath = bgp_info_mpath_next (mpath);
  278. EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
  279. EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
  280. bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
  281. bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
  282. new_best = &test_mp_list_info[0];
  283. old_best = &test_mp_list_info[3];
  284. bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, &mp_cfg);
  285. bgp_mp_list_clear (&mp_list);
  286. EXPECT_TRUE (bgp_info_mpath_count (new_best) == 1, test_result);
  287. mpath = bgp_info_mpath_first (new_best);
  288. EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
  289. EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
  290. EXPECT_TRUE (!CHECK_FLAG (test_mp_list_info[0].flags, BGP_INFO_MULTIPATH),
  291. test_result);
  292. return test_result;
  293. }
  294. static int
  295. cleanup_bgp_info_mpath_update (testcase_t *t)
  296. {
  297. int i;
  298. for (i = 0; i < test_mp_list_peer_count; i++)
  299. sockunion_free (test_mp_list_peer[i].su_remote);
  300. return 0;
  301. }
  302. testcase_t test_bgp_info_mpath_update = {
  303. .desc = "Test bgp_info_mpath_update",
  304. .setup = setup_bgp_info_mpath_update,
  305. .run = run_bgp_info_mpath_update,
  306. .cleanup = cleanup_bgp_info_mpath_update,
  307. };
  308. /*=========================================================
  309. * Set up testcase vector
  310. */
  311. testcase_t *all_tests[] = {
  312. &test_bgp_cfg_maximum_paths,
  313. &test_bgp_mp_list,
  314. &test_bgp_info_mpath_update,
  315. };
  316. int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *));
  317. /*=========================================================
  318. * Test Driver Functions
  319. */
  320. static int
  321. global_test_init (void)
  322. {
  323. master = thread_master_create ();
  324. zclient = zclient_new (master);
  325. bgp_master_init ();
  326. bgp_option_set (BGP_OPT_NO_LISTEN);
  327. if (fileno (stdout) >= 0)
  328. tty = isatty (fileno (stdout));
  329. return 0;
  330. }
  331. static int
  332. global_test_cleanup (void)
  333. {
  334. zclient_free (zclient);
  335. thread_master_free (master);
  336. return 0;
  337. }
  338. static void
  339. display_result (testcase_t *test, int result)
  340. {
  341. if (tty)
  342. printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED);
  343. else
  344. printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED");
  345. }
  346. static int
  347. setup_test (testcase_t *t)
  348. {
  349. int res = 0;
  350. if (t->setup)
  351. res = t->setup (t);
  352. return res;
  353. }
  354. static int
  355. cleanup_test (testcase_t *t)
  356. {
  357. int res = 0;
  358. if (t->cleanup)
  359. res = t->cleanup (t);
  360. return res;
  361. }
  362. static void
  363. run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count)
  364. {
  365. int test_index, result;
  366. testcase_t *cur_test;
  367. *pass_count = *fail_count = 0;
  368. for (test_index = 0; test_index < num_tests; test_index++)
  369. {
  370. cur_test = tests[test_index];
  371. if (!cur_test->desc)
  372. {
  373. printf ("error: test %d has no description!\n", test_index);
  374. continue;
  375. }
  376. if (!cur_test->run)
  377. {
  378. printf ("error: test %s has no run function!\n", cur_test->desc);
  379. continue;
  380. }
  381. if (setup_test (cur_test) != 0)
  382. {
  383. printf ("error: setup failed for test %s\n", cur_test->desc);
  384. continue;
  385. }
  386. result = cur_test->run (cur_test);
  387. if (result == TEST_PASSED)
  388. *pass_count += 1;
  389. else
  390. *fail_count += 1;
  391. display_result (cur_test, result);
  392. if (cleanup_test (cur_test) != 0)
  393. {
  394. printf ("error: cleanup failed for test %s\n", cur_test->desc);
  395. continue;
  396. }
  397. }
  398. }
  399. int
  400. main (void)
  401. {
  402. int pass_count, fail_count;
  403. time_t cur_time;
  404. time (&cur_time);
  405. printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
  406. if (global_test_init () != 0)
  407. {
  408. printf("Global init failed. Terminating.\n");
  409. exit(1);
  410. }
  411. run_tests (all_tests, all_tests_count, &pass_count, &fail_count);
  412. global_test_cleanup ();
  413. printf("Total pass/fail: %d/%d\n", pass_count, fail_count);
  414. return fail_count;
  415. }