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