]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/bgp_local_asn/test_bgp_local_asn_vrf_topo2.py
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / tests / topotests / bgp_local_asn / test_bgp_local_asn_vrf_topo2.py
1 #!/usr/bin/env python3
2 #
3 # Copyright (c) 2022 by VMware, Inc. ("VMware")
4 # Used Copyright (c) 2018 by Network Device Education Foundation,
5 # Inc. ("NetDEF") in this file.
6 #
7 # Permission to use, copy, modify, and/or distribute this software
8 # for any purpose with or without fee is hereby granted, provided
9 # that the above copyright notice and this permission notice appear
10 # in all copies.
11 #
12 # THE SOFTWARE IS PROVIDED "AS IS" AND VMWARE DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL VMWARE BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
16 # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
17 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18 # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
19 # OF THIS SOFTWARE.
20 #
21
22 """
23 1. Verify the BGP Local AS functionality by adding new AS when leaking routes
24 from non-default VRF to non-default with route map by prefix lists.
25 """
26
27 import os
28 import sys
29 import time
30 import pytest
31
32 # Save the Current Working Directory to find configuration files.
33 CWD = os.path.dirname(os.path.realpath(__file__))
34 sys.path.append(os.path.join(CWD, "../"))
35 sys.path.append(os.path.join(CWD, "../lib/"))
36
37 # pylint: disable=C0413
38 # Import topogen and topotest helpers
39 from lib.topogen import Topogen, get_topogen
40 from lib.topotest import version_cmp
41
42 from lib.common_config import (
43 start_topology,
44 write_test_header,
45 create_static_routes,
46 write_test_footer,
47 reset_config_on_routers,
48 verify_rib,
49 step,
50 check_address_types,
51 check_router_status,
52 create_static_routes,
53 create_prefix_lists,
54 verify_fib_routes,
55 create_route_maps,
56 )
57
58 from lib.topolog import logger
59 from lib.bgp import (
60 verify_bgp_convergence,
61 verify_bgp_rib,
62 create_router_bgp,
63 )
64 from lib.topojson import build_config_from_json
65
66 pytestmark = [pytest.mark.bgpd, pytest.mark.staticd]
67
68 # Global variables
69 BGP_CONVERGENCE = False
70 ADDR_TYPES = check_address_types()
71 NETWORK = {"ipv4": "10.1.1.0/32", "ipv6": "10:1::1:0/128"}
72 NEXT_HOP_IP = {"ipv4": "Null0", "ipv6": "Null0"}
73
74
75 def setup_module(mod):
76 """
77 Sets up the pytest environment
78
79 * `mod`: module name
80 """
81
82 testsuite_run_time = time.asctime(time.localtime(time.time()))
83 logger.info("Testsuite start time: {}".format(testsuite_run_time))
84 logger.info("=" * 40)
85
86 logger.info("Running setup_module to create topology")
87
88 # This function initiates the topology build with Topogen...
89 json_file = "{}/bgp_local_asn_vrf_topo2.json".format(CWD)
90 tgen = Topogen(json_file, mod.__name__)
91 global topo
92 topo = tgen.json_topo
93 # ... and here it calls Mininet initialization functions.
94
95 # Starting topology, create tmp files which are loaded to routers
96 # to start daemons and then start routers
97 start_topology(tgen)
98
99 # Creating configuration from JSON
100 build_config_from_json(tgen, topo)
101
102 global BGP_CONVERGENCE
103 global ADDR_TYPES
104 ADDR_TYPES = check_address_types()
105
106 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
107 assert BGP_CONVERGENCE is True, "setup_module : Failed \n Error: {}".format(
108 BGP_CONVERGENCE
109 )
110
111 logger.info("Running setup_module() done")
112
113
114 def teardown_module():
115 """Teardown the pytest environment"""
116
117 logger.info("Running teardown_module to delete topology")
118
119 tgen = get_topogen()
120
121 # Stop toplogy and Remove tmp files
122 tgen.stop_topology()
123
124 logger.info(
125 "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
126 )
127 logger.info("=" * 40)
128
129
130 ################################################################################################
131 #
132 # Testcases
133 #
134 ###############################################################################################
135
136
137 def test_verify_local_asn_ipv4_import_from_non_default_to_non_default_VRF_p0(request):
138 """
139 Verify the BGP Local AS functionality by adding new AS when leaking routes
140 from non-default VRF to non-default with route map by prefix lists.
141 """
142 tgen = get_topogen()
143 global BGP_CONVERGENCE
144 if BGP_CONVERGENCE != True:
145 pytest.skip("skipped because of BGP Convergence failure")
146
147 # test case name
148 tc_name = request.node.name
149 write_test_header(tc_name)
150 if tgen.routers_have_failure():
151 check_router_status(tgen)
152 reset_config_on_routers(tgen)
153
154 step("Base config is done as part of JSON")
155
156 # configure static routes
157 step("Advertise prefix 10.1.1.0/32 from Router-1(AS-100).")
158 step("Advertise an ipv6 prefix 10:1::1:0/128 from Router-1(AS-100).")
159 dut = "r2"
160 for addr_type in ADDR_TYPES:
161 # Enable static routes
162 input_dict_static_route = {
163 "r2": {
164 "static_routes": [
165 {
166 "network": NETWORK[addr_type],
167 "next_hop": NEXT_HOP_IP[addr_type],
168 "vrf": "RED",
169 }
170 ]
171 }
172 }
173
174 logger.info("Configure static routes")
175 result = create_static_routes(tgen, input_dict_static_route)
176 assert result is True, "Testcase {} : Failed \n Error: {}".format(
177 tc_name, result
178 )
179
180 step("configure redistribute static in Router BGP in R2")
181 input_dict_static_route_redist = {
182 "r2": {
183 "bgp": {
184 "local_as": 200,
185 "vrf": "RED",
186 "address_family": {
187 "ipv4": {
188 "unicast": {
189 "redistribute": [
190 {"redist_type": "static"},
191 {"redist_type": "connected"},
192 ]
193 }
194 },
195 "ipv6": {
196 "unicast": {
197 "redistribute": [
198 {"redist_type": "static"},
199 {"redist_type": "connected"},
200 ]
201 }
202 },
203 },
204 }
205 }
206 }
207 result = create_router_bgp(tgen, topo, input_dict_static_route_redist)
208 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
209
210 step("Configure import vrf BLUE from vrf RED on R3 under IPv4 Address Family")
211 input_import_vrf_ipv4 = {
212 "r3": {
213 "bgp": [
214 {
215 "local_as": 300,
216 "vrf": "BLUE",
217 "address_family": {
218 "ipv4": {"unicast": {"import": {"vrf": "RED"}}},
219 "ipv6": {"unicast": {"import": {"vrf": "RED"}}},
220 },
221 }
222 ]
223 }
224 }
225 result = create_router_bgp(tgen, topo, input_import_vrf_ipv4)
226 assert result is True, "Testcase {} : Failed \n Error: {}".format(tc_name, result)
227
228 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
229 for addr_type in ADDR_TYPES:
230 static_routes_input = {
231 "r2": {
232 "static_routes": [
233 {
234 "network": NETWORK[addr_type],
235 "next_hop": NEXT_HOP_IP[addr_type],
236 "vrf": "BLUE",
237 }
238 ]
239 }
240 }
241 for dut in ["r3", "r4"]:
242 result = verify_fib_routes(tgen, addr_type, dut, static_routes_input)
243 assert result is True, "Testcase {} : Failed \n Error: {}".format(
244 tc_name, result
245 )
246 result = verify_bgp_rib(tgen, addr_type, dut, static_routes_input)
247 assert result is True, "Testcase {} : Failed \n Error: {}".format(
248 tc_name, result
249 )
250
251 step("Configure local-as at R3 towards R2.")
252 for addr_type in ADDR_TYPES:
253 input_dict_r3_to_r2 = {
254 "r3": {
255 "vrfs": [{"name": "RED", "id": "1"}],
256 "bgp": [
257 {
258 "local_as": "300",
259 "vrf": "RED",
260 "address_family": {
261 addr_type: {
262 "unicast": {
263 "neighbor": {
264 "r2": {
265 "dest_link": {
266 "r3": {"local_asn": {"local_as": "110"}}
267 }
268 }
269 }
270 }
271 }
272 },
273 }
274 ],
275 }
276 }
277 result = create_router_bgp(tgen, topo, input_dict_r3_to_r2)
278 assert result is True, "Testcase {} :Failed \n Error: {}".format(
279 tc_name, result
280 )
281
282 step("Configure local-as at R3 towards R4.")
283 for addr_type in ADDR_TYPES:
284 input_dict_r3_to_r4 = {
285 "r3": {
286 "vrfs": [{"name": "BLUE", "id": "1"}],
287 "bgp": [
288 {
289 "local_as": "300",
290 "vrf": "BLUE",
291 "address_family": {
292 addr_type: {
293 "unicast": {
294 "neighbor": {
295 "r4": {
296 "dest_link": {
297 "r3": {"local_asn": {"local_as": "110"}}
298 }
299 }
300 }
301 }
302 }
303 },
304 }
305 ],
306 }
307 }
308 result = create_router_bgp(tgen, topo, input_dict_r3_to_r4)
309 assert result is True, "Testcase {} :Failed \n Error: {}".format(
310 tc_name, result
311 )
312
313 step("Configure remote-as at R2 towards R3.")
314 for addr_type in ADDR_TYPES:
315 input_dict_r2_to_r3 = {
316 "r2": {
317 "vrfs": [{"name": "RED", "id": "1"}],
318 "bgp": [
319 {
320 "local_as": "200",
321 "vrf": "RED",
322 "address_family": {
323 addr_type: {
324 "unicast": {
325 "neighbor": {
326 "r3": {
327 "dest_link": {
328 "r2": {
329 "local_asn": {"remote_as": "110"}
330 }
331 }
332 }
333 }
334 }
335 }
336 },
337 }
338 ],
339 }
340 }
341 result = create_router_bgp(tgen, topo, input_dict_r2_to_r3)
342 assert result is True, "Testcase {} :Failed \n Error: {}".format(
343 tc_name, result
344 )
345
346 step("Configure remote-as at R4 towards R3.")
347 for addr_type in ADDR_TYPES:
348 input_dict_r4_to_r3 = {
349 "r4": {
350 "vrfs": [{"name": "BLUE", "id": "1"}],
351 "bgp": [
352 {
353 "local_as": "400",
354 "vrf": "BLUE",
355 "address_family": {
356 addr_type: {
357 "unicast": {
358 "neighbor": {
359 "r3": {
360 "dest_link": {
361 "r4": {
362 "local_asn": {"remote_as": "110"}
363 }
364 }
365 }
366 }
367 }
368 }
369 },
370 }
371 ],
372 }
373 }
374 result = create_router_bgp(tgen, topo, input_dict_r4_to_r3)
375 assert result is True, "Testcase {} :Failed \n Error: {}".format(
376 tc_name, result
377 )
378
379 step("BGP neighborship is verified by following commands in R3 routers")
380 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
381 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
382 BGP_CONVERGENCE
383 )
384
385 step("Verify IPv4 and IPv6 static routes received on R3 VRF BLUE & R4 VRF BLUE")
386 for addr_type in ADDR_TYPES:
387 static_routes_input = {
388 "r2": {
389 "static_routes": [
390 {
391 "network": NETWORK[addr_type],
392 "next_hop": NEXT_HOP_IP[addr_type],
393 "vrf": "BLUE",
394 }
395 ]
396 }
397 }
398 for dut in ["r3", "r4"]:
399 result = verify_fib_routes(tgen, addr_type, dut, static_routes_input)
400 assert result is True, "Testcase {} : Failed \n Error: {}".format(
401 tc_name, result
402 )
403
404 result = verify_bgp_rib(tgen, addr_type, dut, static_routes_input)
405 assert result is True, "Testcase {} : Failed \n Error: {}".format(
406 tc_name, result
407 )
408
409 step(
410 "Verify that AS-110 is got added in the AS list 110 200 100 by following "
411 " commands at R3 router."
412 )
413 dut = "r3"
414 aspath = "110 200"
415 for addr_type in ADDR_TYPES:
416 input_static_r2 = {
417 "r2": {"static_routes": [{"network": NETWORK[addr_type], "vrf": "BLUE"}]}
418 }
419 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r2, aspath=aspath)
420 assert result is True, "Testcase {} : Failed \n Error: {}".format(
421 tc_name, result
422 )
423
424 # configure the prefix-list and route-maps.
425 for adt in ADDR_TYPES:
426 # Create Static routes
427 input_dict_rm1 = {
428 "r2": {
429 "static_routes": [
430 {
431 "network": NETWORK[adt],
432 "no_of_ip": 1,
433 "next_hop": NEXT_HOP_IP[adt],
434 "vrf": "RED",
435 }
436 ]
437 }
438 }
439
440 result = create_static_routes(tgen, input_dict_rm1)
441 assert result is True, "Testcase {} : Failed \n Error: {}".format(
442 tc_name, result
443 )
444
445 # Api call to redistribute static routes
446 input_dict_rm_rd = {
447 "r2": {
448 "bgp": {
449 "local_as": 200,
450 "address_family": {
451 "ipv4": {
452 "unicast": {
453 "redistribute": [
454 {"redist_type": "static"},
455 {"redist_type": "connected"},
456 ]
457 }
458 },
459 "ipv6": {
460 "unicast": {
461 "redistribute": [
462 {"redist_type": "static"},
463 {"redist_type": "connected"},
464 ]
465 }
466 },
467 },
468 }
469 }
470 }
471
472 result = create_router_bgp(tgen, topo, input_dict_rm_rd)
473 assert result is True, "Testcase {} : Failed \n Error: {}".format(
474 tc_name, result
475 )
476
477 input_dict_rm_pl = {
478 "r3": {
479 "prefix_lists": {
480 "ipv4": {
481 "pf_list_1_ipv4": [
482 {
483 "seqid": 10,
484 "action": "permit",
485 "network": NETWORK["ipv4"],
486 }
487 ]
488 },
489 "ipv6": {
490 "pf_list_1_ipv6": [
491 {
492 "seqid": 100,
493 "action": "permit",
494 "network": NETWORK["ipv6"],
495 }
496 ]
497 },
498 }
499 }
500 }
501 result = create_prefix_lists(tgen, input_dict_rm_pl)
502 assert result is True, "Testcase {} : Failed \n Error: {}".format(
503 tc_name, result
504 )
505
506 # Create route map
507 for addr_type in ADDR_TYPES:
508 input_dict_rm_r3 = {
509 "r3": {
510 "route_maps": {
511 "rmap_match_tag_1_{}".format(addr_type): [
512 {
513 "action": "permit",
514 "match": {
515 addr_type: {
516 "prefix_lists": "pf_list_1_{}".format(addr_type)
517 }
518 },
519 }
520 ],
521 "rmap_match_tag_2_{}".format(addr_type): [
522 {
523 "action": "permit",
524 "match": {
525 addr_type: {
526 "prefix_lists": "pf_list_2_{}".format(addr_type)
527 }
528 },
529 }
530 ],
531 }
532 }
533 }
534 result = create_route_maps(tgen, input_dict_rm_r3)
535 assert result is True, "Testcase {} : Failed \n Error: {}".format(
536 tc_name, result
537 )
538
539 # Configure neighbor for route map
540 input_dict_conf_neighbor_rm = {
541 "r3": {
542 "bgp": [
543 {
544 "local_as": "300",
545 "vrf": "BLUE",
546 "address_family": {
547 "ipv4": {
548 "unicast": {
549 "neighbor": {
550 "r4": {
551 "dest_link": {
552 "r3": {
553 "route_maps": [
554 {
555 "name": "rmap_match_tag_1_ipv4",
556 "direction": "out",
557 },
558 {
559 "name": "rmap_match_tag_1_ipv4",
560 "direction": "out",
561 },
562 ]
563 }
564 }
565 }
566 }
567 }
568 },
569 "ipv6": {
570 "unicast": {
571 "neighbor": {
572 "r4": {
573 "dest_link": {
574 "r3": {
575 "route_maps": [
576 {
577 "name": "rmap_match_tag_1_ipv6",
578 "direction": "in",
579 },
580 {
581 "name": "rmap_match_tag_1_ipv6",
582 "direction": "out",
583 },
584 ]
585 }
586 }
587 }
588 }
589 }
590 },
591 },
592 }
593 ]
594 }
595 }
596
597 result = create_router_bgp(tgen, topo, input_dict_conf_neighbor_rm)
598 assert result is True, "Testcase {} : Failed \n Error: {}".format(
599 tc_name, result
600 )
601
602 for adt in ADDR_TYPES:
603 # Verifying RIB routes
604 dut = "r3"
605 input_dict_r2_rib = {
606 "r2": {
607 "static_routes": [
608 {
609 "network": [NETWORK[adt]],
610 "no_of_ip": 1,
611 "next_hop": NEXT_HOP_IP[adt],
612 "vrf": "RED",
613 }
614 ]
615 }
616 }
617 result = verify_rib(tgen, adt, dut, input_dict_r2_rib)
618 assert result is True, "Testcase {}: Failed \n Error: {}".format(
619 tc_name, result
620 )
621
622 # Verifying RIB routes
623 dut = "r4"
624 input_dict_r2_rib = {
625 "r2": {
626 "static_routes": [
627 {
628 "network": [NETWORK[adt]],
629 "no_of_ip": 1,
630 "next_hop": NEXT_HOP_IP[adt],
631 "vrf": "BLUE",
632 }
633 ]
634 }
635 }
636 result = verify_rib(tgen, adt, dut, input_dict_r2_rib)
637 assert result is True, "Testcase {}: Failed \n Error: {}".format(
638 tc_name, result
639 )
640
641 step("Configure local-as with no-prepend at R3 towards R2.")
642 for addr_type in ADDR_TYPES:
643 input_dict_no_prep_r3_to_r2 = {
644 "r3": {
645 "vrfs": [{"name": "RED", "id": "1"}],
646 "bgp": [
647 {
648 "local_as": "300",
649 "vrf": "RED",
650 "address_family": {
651 addr_type: {
652 "unicast": {
653 "neighbor": {
654 "r2": {
655 "dest_link": {
656 "r3": {
657 "local_asn": {
658 "local_as": "110",
659 "no_prepend": True,
660 }
661 }
662 }
663 }
664 }
665 }
666 }
667 },
668 }
669 ],
670 }
671 }
672 result = create_router_bgp(tgen, topo, input_dict_no_prep_r3_to_r2)
673 assert result is True, "Testcase {} :Failed \n Error: {}".format(
674 tc_name, result
675 )
676
677 step("Configure local-as with no-prepend at R3 towards R4.")
678 for addr_type in ADDR_TYPES:
679 input_dict_no_prep_r3_to_r4 = {
680 "r3": {
681 "vrfs": [{"name": "BLUE", "id": "1"}],
682 "bgp": [
683 {
684 "local_as": "300",
685 "vrf": "BLUE",
686 "address_family": {
687 addr_type: {
688 "unicast": {
689 "neighbor": {
690 "r4": {
691 "dest_link": {
692 "r3": {
693 "local_asn": {
694 "local_as": "110",
695 "no_prepend": True,
696 }
697 }
698 }
699 }
700 }
701 }
702 }
703 },
704 }
705 ],
706 }
707 }
708 result = create_router_bgp(tgen, topo, input_dict_no_prep_r3_to_r4)
709 assert result is True, "Testcase {} :Failed \n Error: {}".format(
710 tc_name, result
711 )
712
713 step("BGP neighborship is verified by following commands in R3 routers")
714 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
715 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
716 BGP_CONVERGENCE
717 )
718
719 dut = "r3"
720 aspath = "200"
721 for addr_type in ADDR_TYPES:
722 input_static_r2 = {
723 "r2": {"static_routes": [{"network": NETWORK[addr_type], "vrf": "BLUE"}]}
724 }
725 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r2, aspath=aspath)
726 assert result is True, "Testcase {} : Failed \n Error: {}".format(
727 tc_name, result
728 )
729
730 step("Configure local-as with no-prepend and replace-as at R3 towards R2")
731 for addr_type in ADDR_TYPES:
732 input_dict_no_prep_rep_as_r3_to_r2 = {
733 "r3": {
734 "vrfs": [{"name": "RED", "id": "1"}],
735 "bgp": [
736 {
737 "local_as": "300",
738 "vrf": "RED",
739 "address_family": {
740 addr_type: {
741 "unicast": {
742 "neighbor": {
743 "r2": {
744 "dest_link": {
745 "r3": {
746 "local_asn": {
747 "local_as": "110",
748 "no_prepend": True,
749 "replace_as": True,
750 }
751 }
752 }
753 }
754 }
755 }
756 }
757 },
758 }
759 ],
760 }
761 }
762 result = create_router_bgp(tgen, topo, input_dict_no_prep_rep_as_r3_to_r2)
763 assert result is True, "Testcase {} :Failed \n Error: {}".format(
764 tc_name, result
765 )
766
767 step("Configure local-as with no-prepend and replace-as at R3 towards R4")
768 for addr_type in ADDR_TYPES:
769 input_dict_no_prep_rep_as_r3_to_r4 = {
770 "r3": {
771 "vrfs": [{"name": "BLUE", "id": "1"}],
772 "bgp": [
773 {
774 "local_as": "300",
775 "vrf": "BLUE",
776 "address_family": {
777 addr_type: {
778 "unicast": {
779 "neighbor": {
780 "r4": {
781 "dest_link": {
782 "r3": {
783 "local_asn": {
784 "local_as": "110",
785 "no_prepend": True,
786 "replace_as": True,
787 }
788 }
789 }
790 }
791 }
792 }
793 }
794 },
795 }
796 ],
797 }
798 }
799 result = create_router_bgp(tgen, topo, input_dict_no_prep_rep_as_r3_to_r4)
800 assert result is True, "Testcase {} :Failed \n Error: {}".format(
801 tc_name, result
802 )
803
804 step("BGP neighborship is verified by following commands in R3 routers")
805 BGP_CONVERGENCE = verify_bgp_convergence(tgen, topo)
806 assert BGP_CONVERGENCE is True, "BGP convergence :Failed \n Error: {}".format(
807 BGP_CONVERGENCE
808 )
809
810 dut = "r4"
811 aspath = "110 200"
812 for addr_type in ADDR_TYPES:
813 input_static_r2 = {
814 "r2": {"static_routes": [{"network": NETWORK[addr_type], "vrf": "BLUE"}]}
815 }
816 result = verify_bgp_rib(tgen, addr_type, dut, input_static_r2, aspath=aspath)
817 assert result is True, "Testcase {} : Failed \n Error: {}".format(
818 tc_name, result
819 )
820
821 write_test_footer(tc_name)
822
823
824 if __name__ == "__main__":
825 args = ["-s"] + sys.argv[1:]
826 sys.exit(pytest.main(args))