bgp_mpath_test.c 14 KB

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