bgp_mpath_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 bgp test_mp_bgp;
  168. struct peer test_mp_list_peer[] = {
  169. { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
  170. { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
  171. { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
  172. { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
  173. { .local_as = 1, .as = 2, .bgp = &test_mp_bgp },
  174. };
  175. int test_mp_list_peer_count = sizeof (test_mp_list_peer)/ sizeof (struct peer);
  176. struct attr test_mp_list_attr[4];
  177. struct bgp_info test_mp_list_info[] = {
  178. { .peer = &test_mp_list_peer[0], .attr = &test_mp_list_attr[0] },
  179. { .peer = &test_mp_list_peer[1], .attr = &test_mp_list_attr[1] },
  180. { .peer = &test_mp_list_peer[2], .attr = &test_mp_list_attr[1] },
  181. { .peer = &test_mp_list_peer[3], .attr = &test_mp_list_attr[2] },
  182. { .peer = &test_mp_list_peer[4], .attr = &test_mp_list_attr[3] },
  183. };
  184. int test_mp_list_info_count =
  185. sizeof (test_mp_list_info)/sizeof (struct bgp_info);
  186. static int
  187. setup_bgp_mp_list (testcase_t *t)
  188. {
  189. test_mp_list_attr[0].nexthop.s_addr = 0x01010101;
  190. test_mp_list_attr[1].nexthop.s_addr = 0x02020202;
  191. test_mp_list_attr[2].nexthop.s_addr = 0x03030303;
  192. test_mp_list_attr[3].nexthop.s_addr = 0x04040404;
  193. if ((test_mp_list_peer[0].su_remote = sockunion_str2su ("1.1.1.1")) == NULL)
  194. return -1;
  195. if ((test_mp_list_peer[1].su_remote = sockunion_str2su ("2.2.2.2")) == NULL)
  196. return -1;
  197. if ((test_mp_list_peer[2].su_remote = sockunion_str2su ("3.3.3.3")) == NULL)
  198. return -1;
  199. if ((test_mp_list_peer[3].su_remote = sockunion_str2su ("4.4.4.4")) == NULL)
  200. return -1;
  201. if ((test_mp_list_peer[4].su_remote = sockunion_str2su ("5.5.5.5")) == NULL)
  202. return -1;
  203. return 0;
  204. }
  205. static int
  206. run_bgp_mp_list (testcase_t *t)
  207. {
  208. struct list mp_list;
  209. struct listnode *mp_node;
  210. struct bgp_info *info;
  211. int i;
  212. int test_result = TEST_PASSED;
  213. bgp_mp_list_init (&mp_list);
  214. EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
  215. bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
  216. bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
  217. bgp_mp_list_add (&mp_list, &test_mp_list_info[2]);
  218. bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
  219. bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
  220. for (i = 0, mp_node = mp_list.head; i < test_mp_list_info_count;
  221. i++, mp_node = listnextnode(mp_node))
  222. {
  223. info = listgetdata(mp_node);
  224. EXPECT_TRUE (info == &test_mp_list_info[i], test_result);
  225. }
  226. bgp_mp_list_clear (&mp_list);
  227. EXPECT_TRUE (listcount(&mp_list) == 0, test_result);
  228. return test_result;
  229. }
  230. static int
  231. cleanup_bgp_mp_list (testcase_t *t)
  232. {
  233. int i;
  234. for (i = 0; i < test_mp_list_peer_count; i++)
  235. sockunion_free (test_mp_list_peer[i].su_remote);
  236. return 0;
  237. }
  238. testcase_t test_bgp_mp_list = {
  239. .desc = "Test bgp_mp_list",
  240. .setup = setup_bgp_mp_list,
  241. .run = run_bgp_mp_list,
  242. .cleanup = cleanup_bgp_mp_list,
  243. };
  244. /*=========================================================
  245. * Testcase for bgp_info_mpath_update
  246. */
  247. struct bgp_node test_rn;
  248. static int
  249. setup_bgp_info_mpath_update (testcase_t *t)
  250. {
  251. int i;
  252. str2prefix ("42.1.1.0/24", &test_rn.p);
  253. setup_bgp_mp_list (t);
  254. for (i = 0; i < test_mp_list_info_count; i++)
  255. bgp_info_add (&test_rn, &test_mp_list_info[i]);
  256. return 0;
  257. }
  258. static int
  259. run_bgp_info_mpath_update (testcase_t *t)
  260. {
  261. struct bgp_info *new_best, *old_best, *mpath;
  262. struct list mp_list;
  263. test_mp_bgp.maxpaths[AFI_IP][SAFI_UNICAST].maxpaths_ebgp = 3;
  264. test_mp_bgp.maxpaths[AFI_IP][SAFI_UNICAST].maxpaths_ibgp = 3;
  265. int test_result = TEST_PASSED;
  266. bgp_mp_list_init (&mp_list);
  267. bgp_mp_list_add (&mp_list, &test_mp_list_info[4]);
  268. bgp_mp_list_add (&mp_list, &test_mp_list_info[3]);
  269. bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
  270. bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
  271. new_best = &test_mp_list_info[3];
  272. old_best = NULL;
  273. bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, AFI_IP, SAFI_UNICAST);
  274. bgp_mp_list_clear (&mp_list);
  275. EXPECT_TRUE (bgp_info_mpath_count (new_best) == 2, test_result);
  276. mpath = bgp_info_mpath_first (new_best);
  277. EXPECT_TRUE (mpath == &test_mp_list_info[0], test_result);
  278. EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
  279. mpath = bgp_info_mpath_next (mpath);
  280. EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
  281. EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
  282. bgp_mp_list_add (&mp_list, &test_mp_list_info[0]);
  283. bgp_mp_list_add (&mp_list, &test_mp_list_info[1]);
  284. new_best = &test_mp_list_info[0];
  285. old_best = &test_mp_list_info[3];
  286. bgp_info_mpath_update (&test_rn, new_best, old_best, &mp_list, AFI_IP, SAFI_UNICAST);
  287. bgp_mp_list_clear (&mp_list);
  288. EXPECT_TRUE (bgp_info_mpath_count (new_best) == 1, test_result);
  289. mpath = bgp_info_mpath_first (new_best);
  290. EXPECT_TRUE (mpath == &test_mp_list_info[1], test_result);
  291. EXPECT_TRUE (CHECK_FLAG (mpath->flags, BGP_INFO_MULTIPATH), test_result);
  292. EXPECT_TRUE (!CHECK_FLAG (test_mp_list_info[0].flags, BGP_INFO_MULTIPATH),
  293. test_result);
  294. return test_result;
  295. }
  296. static int
  297. cleanup_bgp_info_mpath_update (testcase_t *t)
  298. {
  299. int i;
  300. for (i = 0; i < test_mp_list_peer_count; i++)
  301. sockunion_free (test_mp_list_peer[i].su_remote);
  302. return 0;
  303. }
  304. testcase_t test_bgp_info_mpath_update = {
  305. .desc = "Test bgp_info_mpath_update",
  306. .setup = setup_bgp_info_mpath_update,
  307. .run = run_bgp_info_mpath_update,
  308. .cleanup = cleanup_bgp_info_mpath_update,
  309. };
  310. /*=========================================================
  311. * Set up testcase vector
  312. */
  313. testcase_t *all_tests[] = {
  314. &test_bgp_cfg_maximum_paths,
  315. &test_bgp_mp_list,
  316. &test_bgp_info_mpath_update,
  317. };
  318. int all_tests_count = (sizeof(all_tests)/sizeof(testcase_t *));
  319. /*=========================================================
  320. * Test Driver Functions
  321. */
  322. static int
  323. global_test_init (void)
  324. {
  325. master = thread_master_create ();
  326. zclient = zclient_new (master);
  327. bgp_master_init ();
  328. bgp_option_set (BGP_OPT_NO_LISTEN);
  329. if (fileno (stdout) >= 0)
  330. tty = isatty (fileno (stdout));
  331. return 0;
  332. }
  333. static int
  334. global_test_cleanup (void)
  335. {
  336. zclient_free (zclient);
  337. thread_master_free (master);
  338. return 0;
  339. }
  340. static void
  341. display_result (testcase_t *test, int result)
  342. {
  343. if (tty)
  344. printf ("%s: %s\n", test->desc, result == TEST_PASSED ? OK : FAILED);
  345. else
  346. printf ("%s: %s\n", test->desc, result == TEST_PASSED ? "OK" : "FAILED");
  347. }
  348. static int
  349. setup_test (testcase_t *t)
  350. {
  351. int res = 0;
  352. if (t->setup)
  353. res = t->setup (t);
  354. return res;
  355. }
  356. static int
  357. cleanup_test (testcase_t *t)
  358. {
  359. int res = 0;
  360. if (t->cleanup)
  361. res = t->cleanup (t);
  362. return res;
  363. }
  364. static void
  365. run_tests (testcase_t *tests[], int num_tests, int *pass_count, int *fail_count)
  366. {
  367. int test_index, result;
  368. testcase_t *cur_test;
  369. *pass_count = *fail_count = 0;
  370. for (test_index = 0; test_index < num_tests; test_index++)
  371. {
  372. cur_test = tests[test_index];
  373. if (!cur_test->desc)
  374. {
  375. printf ("error: test %d has no description!\n", test_index);
  376. continue;
  377. }
  378. if (!cur_test->run)
  379. {
  380. printf ("error: test %s has no run function!\n", cur_test->desc);
  381. continue;
  382. }
  383. if (setup_test (cur_test) != 0)
  384. {
  385. printf ("error: setup failed for test %s\n", cur_test->desc);
  386. continue;
  387. }
  388. result = cur_test->run (cur_test);
  389. if (result == TEST_PASSED)
  390. *pass_count += 1;
  391. else
  392. *fail_count += 1;
  393. display_result (cur_test, result);
  394. if (cleanup_test (cur_test) != 0)
  395. {
  396. printf ("error: cleanup failed for test %s\n", cur_test->desc);
  397. continue;
  398. }
  399. }
  400. }
  401. int
  402. main (void)
  403. {
  404. int pass_count, fail_count;
  405. time_t cur_time;
  406. time (&cur_time);
  407. printf("BGP Multipath Tests Run at %s", ctime(&cur_time));
  408. if (global_test_init () != 0)
  409. {
  410. printf("Global init failed. Terminating.\n");
  411. exit(1);
  412. }
  413. run_tests (all_tests, all_tests_count, &pass_count, &fail_count);
  414. global_test_cleanup ();
  415. printf("Total pass/fail: %d/%d\n", pass_count, fail_count);
  416. return fail_count;
  417. }