summaryrefslogtreecommitdiffstats
path: root/tests/topotests/multicast_pim_bsm_topo2/test_mcast_pim_bsmp_02.py
blob: e4df5089099fa301ed4d7fa9ce75c8901e41d8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
#!/usr/bin/env python
# SPDX-License-Identifier: ISC
#
# Copyright (c) 2020 by VMware, Inc. ("VMware")
# Used Copyright (c) 2018 by Network Device Education Foundation,
# Inc. ("NetDEF") in this file.
#

"""
Following tests are covered to test PIM BSM processing basic functionality:

Test steps
- Create topology (setup module)
- Bring up topology

Tests covered in this suite
1. Verify (*,G) mroute detail on FRR router after BSM rp installed
2. Verify group to RP updated correctly on FRR router, when BSR advertising
    the overlapping group address
3. Verify group to RP info is updated correctly, when BSR advertising the
    same RP with different priority
4. Verify group to RP mapping in FRR node when 2 BSR are present in the network
    and both are having same BSR priority
5. Verify RP is selected based on hash function, when BSR advertising the group
    to RP mapping with same priority
6. Verify fragmentation of bootstrap message
7. Verify when candidate RP advertised with 32 mask length
    and contain all the contacts
"""

import os
import sys
import time
import pytest

# Save the Current Working Directory to find configuration files.
CWD = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(CWD, "../"))
sys.path.append(os.path.join(CWD, "../lib/"))

# Required to instantiate the topology builder class.

# pylint: disable=C0413
# Import topogen and topotest helpers
from lib.topogen import Topogen, get_topogen

from lib.common_config import (
    start_topology,
    write_test_header,
    write_test_footer,
    step,
    addKernelRoute,
    create_static_routes,
    reset_config_on_routers,
    run_frr_cmd,
    required_linux_kernel_version,
    verify_rib,
)

from lib.pim import (
    add_rp_interfaces_and_pim_config,
    reconfig_interfaces,
    scapy_send_bsr_raw_packet,
    find_rp_from_bsrp_info,
    verify_pim_grp_rp_source,
    verify_pim_bsr,
    verify_mroutes,
    verify_join_state_and_timer,
    verify_pim_state,
    verify_upstream_iif,
    verify_igmp_groups,
    verify_pim_upstream_rpf,
    clear_mroute,
    clear_pim_interface_traffic,
    McastTesterHelper,
    verify_pim_neighbors,
)
from lib.topolog import logger
from lib.topojson import build_config_from_json

pytestmark = [pytest.mark.pimd, pytest.mark.staticd]


TOPOLOGY = """

      b1_____
             |
             |
      s1-----f1-----i1-----l1----r1
             |
       ______|
      b2

      b1 - BSR 1
      b2 - BSR 2
      s1 - Source
      f1 - FHR
      i1 - Intermediate Router (also RP)
      r1 - Receiver

"""
# Global variables
NEXT_HOP1 = "70.0.0.1"
NEXT_HOP2 = "65.0.0.1"
BSR_IP_1 = "1.1.2.7"
BSR_IP_2 = "10.2.1.1"
BSR1_ADDR = "1.1.2.7/32"
BSR2_ADDR = "10.2.1.1/32"


def setup_module(mod):
    """
    Sets up the pytest environment

    * `mod`: module name
    """

    # Required linux kernel version for this suite to run.
    result = required_linux_kernel_version("4.15")
    if result is not True:
        pytest.skip("Kernel version should be >= 4.15")

    testsuite_run_time = time.asctime(time.localtime(time.time()))
    logger.info("Testsuite start time: {}".format(testsuite_run_time))
    logger.info("=" * 40)
    logger.info("Master Topology: \n {}".format(TOPOLOGY))

    logger.info("Running setup_module to create topology")

    # This function initiates the topology build with Topogen...
    json_file = "{}/mcast_pim_bsmp_02.json".format(CWD)
    tgen = Topogen(json_file, mod.__name__)
    global topo
    topo = tgen.json_topo
    # ... and here it calls Mininet initialization functions.

    # Starting topology, create tmp files which are loaded to routers
    #  to start daemons and then start routers
    start_topology(tgen)

    # Don"t run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    # Creating configuration from JSON
    build_config_from_json(tgen, topo)

    # Verify PIM neighbors
    result = verify_pim_neighbors(tgen, topo)
    assert result is True, " Verify PIM neighbor: Failed Error: {}".format(result)

    # XXX Replace this using "with McastTesterHelper()... " in each test if possible.
    global app_helper
    app_helper = McastTesterHelper(tgen)

    logger.info("Running setup_module() done")


def teardown_module():
    """Teardown the pytest environment"""

    logger.info("Running teardown_module to delete topology")

    tgen = get_topogen()

    app_helper.cleanup()

    # Stop toplogy and Remove tmp files
    tgen.stop_topology()

    logger.info(
        "Testsuite end time: {}".format(time.asctime(time.localtime(time.time())))
    )
    logger.info("=" * 40)


#####################################################
#
#   Local APIs
#
#####################################################


def clear_bsrp_data(tgen, topo):
    """
    clear bsm databas after test"
    Parameters
    ----------
    * `tgen`: topogen object

    Usage
    -----
    result = clear_bsrp_data(tgen, topo)
    Returns
    -------
    errormsg(str) or True
    """

    for dut in tgen.routers():
        rnode = tgen.routers()[dut]

        logger.info("[DUT: %s]: clear_bsrp_data")

        run_frr_cmd(rnode, "clear ip pim bsr-data")

    return True


def pre_config_to_bsm(tgen, topo, tc_name, bsr, sender, receiver, fhr, rp, lhr, packet):
    """
    API to do required configuration to send and receive BSR packet
    """

    # Re-configure interfaces as per BSR packet
    result = reconfig_interfaces(tgen, topo, bsr, fhr, packet)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Create static routes
    if "bsr" in topo["routers"][bsr]["bsm"]["bsr_packets"][packet]:
        bsr_route = topo["routers"][bsr]["bsm"]["bsr_packets"][packet]["bsr"]
        next_hop = topo["routers"][bsr]["bsm"]["bsr_packets"][packet]["src_ip"].split(
            "/"
        )[0]
        next_hop_rp = topo["routers"][fhr]["links"][rp]["ipv4"].split("/")[0]
        next_hop_lhr = topo["routers"][rp]["links"][lhr]["ipv4"].split("/")[0]

        # Add static routes
        input_dict = {
            rp: {"static_routes": [{"network": bsr_route, "next_hop": next_hop_rp}]},
            lhr: {"static_routes": [{"network": bsr_route, "next_hop": next_hop_lhr}]},
        }

        result = create_static_routes(tgen, input_dict)
        assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

        # Verifying static routes are installed
        for dut, _nexthop in zip([rp, lhr], [next_hop_rp, next_hop_lhr]):
            input_routes = {dut: input_dict[dut]}
            result = verify_rib(
                tgen, "ipv4", dut, input_routes, _nexthop, protocol="static"
            )
            assert result is True, "Testcase {} : Failed \n Error {}".format(
                tc_name, result
            )

    # Add kernel route for source
    group = topo["routers"][bsr]["bsm"]["bsr_packets"][packet]["pkt_dst"]
    bsr_interface = topo["routers"][bsr]["links"][fhr]["interface"]
    result = addKernelRoute(tgen, bsr, bsr_interface, group)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # RP Mapping
    rp_mapping = topo["routers"][bsr]["bsm"]["bsr_packets"][packet]["rp_mapping"]

    # Add interfaces in RP for all the RPs
    result = add_rp_interfaces_and_pim_config(tgen, topo, "lo", rp, rp_mapping)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Add kernel routes to sender and receiver
    for group, rp_list in rp_mapping.items():
        mask = group.split("/")[1]
        if int(mask) == 32:
            group = group.split("/")[0]

        # Add kernel routes for sender
        s_interface = topo["routers"][sender]["links"][fhr]["interface"]
        result = addKernelRoute(tgen, sender, s_interface, group)
        assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

        # Add kernel routes for receiver
        r_interface = topo["routers"][receiver]["links"][lhr]["interface"]
        result = addKernelRoute(tgen, receiver, r_interface, group)
        assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

        # Add static routes for RPs in FHR and LHR
        next_hop_fhr = topo["routers"][rp]["links"][fhr]["ipv4"].split("/")[0]
        next_hop_lhr = topo["routers"][rp]["links"][lhr]["ipv4"].split("/")[0]
        input_dict = {
            fhr: {"static_routes": [{"network": rp_list, "next_hop": next_hop_fhr}]},
        }
        result = create_static_routes(tgen, input_dict)
        assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

        # Verifying static routes are installed
        result = verify_rib(
            tgen, "ipv4", fhr, input_dict, next_hop_fhr, protocol="static"
        )
        assert result is True, "Testcase {} : Failed \n Error {}".format(
            tc_name, result
        )

        input_dict = {
            lhr: {"static_routes": [{"network": rp_list, "next_hop": next_hop_lhr}]},
        }
        result = create_static_routes(tgen, input_dict)
        assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

        # Verifying static routes are installed
        result = verify_rib(
            tgen, "ipv4", lhr, input_dict, next_hop_lhr, protocol="static"
        )
        assert result is True, "Testcase {} : Failed \n Error {}".format(
            tc_name, result
        )

    return True


#####################################################
#
#   Testcases
#
#####################################################


def test_starg_mroute_p0(request):
    """
    1. Verify (*,G) mroute detail on FRR router after BSM rp installed

    Topology used:
      b1_____
             |
             |
      s1-----f1-----i1-----l1----r1
             |
       ______|
      b2

      b1 - BSR 1
      b2 - BSR 2
      s1 - Source
      f1 - FHR
      i1 - Intermediate Router (also RP)
      r1 - Receiver

    """

    tgen = get_topogen()
    tc_name = request.node.name
    write_test_header(tc_name)

    # Don"t run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    app_helper.stop_all_hosts()
    clear_mroute(tgen)
    reset_config_on_routers(tgen)
    clear_pim_interface_traffic(tgen, topo)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b1", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b2", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    GROUP_ADDRESS = "226.1.1.1"

    # Use scapy to send pre-defined packet from senser to receiver
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet1")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    bsr_ip = topo["routers"]["b1"]["bsm"]["bsr_packets"]["packet1"]["bsr"].split("/")[0]
    time.sleep(1)

    result = app_helper.run_join("r1", GROUP_ADDRESS, "l1")
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify bsr state in FHR
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Check igmp groups
    step("Verify IGMP groups in LHR l1")
    dut = "l1"
    intf = "l1-r1-eth1"
    result = verify_igmp_groups(tgen, dut, intf, GROUP_ADDRESS)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    group = "226.1.1.1/32"
    src_addr = "*"

    # Find the elected rp from bsrp-info
    step("Find the elected rp from bsrp-info in LHR in l1")
    rp = find_rp_from_bsrp_info(tgen, dut, bsr_ip, group)
    assert rp is not {}, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Check RP detail in LHR
    step("Verify RP in LHR in l1")
    result = verify_pim_grp_rp_source(tgen, topo, dut, group, "BSR", rp[group])
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify join state and join timer
    step("Verify join state and join timer in l1")
    iif = "l1-i1-eth0"
    result = verify_join_state_and_timer(tgen, dut, iif, src_addr, GROUP_ADDRESS)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify upstream IIF interface
    step("Verify upstream IIF interface in l1")
    result = verify_upstream_iif(tgen, dut, iif, src_addr, GROUP_ADDRESS)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify IIF/OIL in pim state
    oil = "l1-r1-eth1"
    result = verify_pim_state(tgen, dut, iif, oil, GROUP_ADDRESS)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify ip mroute
    step("Verify ip mroute in l1")
    src_addr = "*"
    result = verify_mroutes(tgen, dut, src_addr, GROUP_ADDRESS, iif, oil)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Remove the group rp mapping and send bsm
    step("Remove the grp-rp mapping by sending bsm with hold time 0 for grp-rp")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet2")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Check RP unreachable
    step("Check RP unreachability in l1")
    iif = "Unknown"
    result = verify_upstream_iif(
        tgen, dut, iif, src_addr, GROUP_ADDRESS, joinState="NotJoined"
    )
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify that it is not installed
    step("Verify that iif is not installed in l1")
    iif = "<iif?>"
    result = verify_pim_state(tgen, dut, iif, oil, GROUP_ADDRESS, installed_fl=0)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify mroute not installed
    step("Verify mroute not installed in l1")
    result = verify_mroutes(
        tgen, dut, src_addr, GROUP_ADDRESS, iif, oil, retry_timeout=20, expected=False
    )
    assert result is not True, (
        "Testcase {} : Failed \n "
        "Expected: [{}]: mroute (S, G) should not be installed \n "
        "Found: {}".format(tc_name, dut, result)
    )

    # Send BSM again to configure rp
    step("Add back RP by sending BSM from b1")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet1")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Verify that (*,G) installed in mroute again
    iif = "l1-i1-eth0"
    result = verify_mroutes(tgen, dut, src_addr, GROUP_ADDRESS, iif, oil)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    step("clear  BSM database before moving to next case")
    clear_bsrp_data(tgen, topo)

    write_test_footer(tc_name)


def test_overlapping_group_p0(request):
    """
    Verify group to RP updated correctly on FRR router, when BSR advertising
    the overlapping group address

    Topology used:
      b1_____
             |
             |
      s1-----f1-----i1-----l1----r1
             |
       ______|
      b2

      b1 - BSR 1
      b2 - BSR 2
      s1 - Source
      f1 - FHR
      i1 - Intermediate Router (also RP)
      r1 - Receiver

    """

    tgen = get_topogen()
    tc_name = request.node.name
    write_test_header(tc_name)

    # Don"t run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    app_helper.stop_all_hosts()
    clear_mroute(tgen)
    reset_config_on_routers(tgen)
    clear_pim_interface_traffic(tgen, topo)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b1", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b2", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    GROUP_ADDRESS = "225.1.1.1"

    # Use scapy to send pre-defined packet from senser to receiver
    step("Send BSR packet from b1 to FHR")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet4")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    bsr_ip = topo["routers"]["b1"]["bsm"]["bsr_packets"]["packet1"]["bsr"].split("/")[0]
    time.sleep(1)

    result = app_helper.run_join("r1", GROUP_ADDRESS, "l1")
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify bsr state in FHR
    step("Verify if b1 is chosen as bsr in f1")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    dut = "l1"
    group1 = "225.1.1.1/32"
    # Find the elected rp from bsrp-info fro group 225.1.1.1/32
    step("Find the elected rp from bsrp-info in LHR for 225.1.1.1/32")
    rp1 = find_rp_from_bsrp_info(tgen, dut, bsr_ip, group1)
    assert rp1 is not {}, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    group2 = "225.1.1.0/24"
    # Find the elected rp from bsrp-info fro group 225.1.1.0/24
    step("Find the elected rp from bsrp-info in LHR for 225.1.1.0/24")
    rp2 = find_rp_from_bsrp_info(tgen, dut, bsr_ip, group2)
    assert rp2 is not {}, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    iif = "l1-i1-eth0"
    # Verify upstream rpf for 225.1.1.1 is chosen as rp1
    step("Verify upstream rpf for 225.1.1.1 is chosen as rp1 in l1")
    result = verify_pim_upstream_rpf(tgen, topo, dut, iif, GROUP_ADDRESS, rp1)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Send BSR packet from b1 with rp for 225.1.1.1/32 removed
    step("Send BSR packet from b1 with rp for 225.1.1.1/32 removed")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet5")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Verify upstream rpf for 225.1.1.1 is chosen as rp1
    step("Verify upstream rpf for 225.1.1.1 is chosen as rp2 in l1")
    result = verify_pim_upstream_rpf(tgen, topo, dut, iif, GROUP_ADDRESS, rp2)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify IIF/OIL in pim state
    step("Verify iif is installed after rp change in l1")
    oil = "l1-r1-eth1"
    result = verify_pim_state(tgen, dut, iif, oil, GROUP_ADDRESS)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    step("clear  BSM database before moving to next case")
    clear_bsrp_data(tgen, topo)

    write_test_footer(tc_name)


def test_RP_priority_p0(request):
    """
    Verify group to RP info is updated correctly, when BSR advertising the
    same RP with different priority

    Topology used:
      b1_____
             |
             |
      s1-----f1-----i1-----l1----r1
             |
       ______|
      b2

      b1 - BSR 1
      b2 - BSR 2
      s1 - Source
      f1 - FHR
      i1 - Intermediate Router (also RP)
      r1 - Receiver
    """

    tgen = get_topogen()
    tc_name = request.node.name
    write_test_header(tc_name)

    # Don"t run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    app_helper.stop_all_hosts()
    clear_mroute(tgen)
    reset_config_on_routers(tgen)
    clear_pim_interface_traffic(tgen, topo)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b1", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b2", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    GROUP_ADDRESS = "225.1.1.1"

    # Use scapy to send pre-defined packet from senser to receiver
    step("Send BSR packet from b1 to FHR")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet1")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    bsr_ip = topo["routers"]["b1"]["bsm"]["bsr_packets"]["packet1"]["bsr"].split("/")[0]
    time.sleep(1)

    result = app_helper.run_join("r1", GROUP_ADDRESS, "l1")
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify bsr state in FHR
    step("Verify if b1 is chosen as bsr in f1")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    dut = "l1"
    group = "225.1.1.0/24"
    # Find the elected rp from bsrp-info
    step("Find the elected rp from bsrp-info in LHR l1")
    rp1 = find_rp_from_bsrp_info(tgen, dut, bsr_ip, group)
    assert rp1 is not {}, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Check RP detail in LHR
    step("Verify RP in LHR l1")
    result = verify_pim_grp_rp_source(tgen, topo, dut, group, "BSR", rp1[group])
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Send BSR packet from b1 after deleting high prio rp for 225.1.1.0/24
    step("Send BSM from b1 to FHR deleting high prio rp for 225.1.1.0/24")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet6")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Find the elected rp from bsrp-info
    step("Find the elected rp from bsrp-info in LHR l1")
    rp2 = find_rp_from_bsrp_info(tgen, dut, bsr_ip, group)
    assert rp2 is not {}, "Testcase {} :Failed \n Error {}".format(tc_name, result)
    logger.info("RP old: {} RP2 new: {} ".format(rp1[group], rp2[group]))

    # Verify is the rp is different now
    assert rp1[group] != rp2[group], "Testcase {} :Failed \n Error {}".format(
        tc_name, result
    )

    rp_add1 = rp1[group]
    rp_add2 = rp2[group]

    # Verify if that rp is installed
    step("Verify new RP in LHR installed")
    result = verify_pim_grp_rp_source(tgen, topo, dut, group, "BSR", rp_add2)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Send BSR packet from b1 after putting back high prio rp for 225.1.1.0/24
    step("Send BSM from b1 to FHR put back old high prio rp for 225.1.1.0/24")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet1")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Find the elected rp from bsrp-info
    step("Find the elected rp from bsrp-info in LHR")
    rp2 = find_rp_from_bsrp_info(tgen, dut, bsr_ip, group)
    assert rp2 is not {}, "Testcase {} :Failed \n Error : RP not Found".format(tc_name)

    # Verify is the rp is different now
    step("Verify now old RP is elected again")
    assert (
        rp_add1 == rp2[group]
    ), "Testcase {} :Failed \n Error : rp expected {} rp received {}".format(
        tc_name, rp_add1, rp2[group] if group in rp2 else None
    )

    # Verify if that rp is installed
    step("Verify new RP in LHR installed")
    result = verify_pim_grp_rp_source(tgen, topo, dut, group, "BSR", rp_add1)
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    step("clear  BSM database before moving to next case")
    clear_bsrp_data(tgen, topo)

    write_test_footer(tc_name)


def test_BSR_election_p0(request):
    """
    Verify group to RP mapping in FRR node when 2 BSR are present in the network
    and both are having same BSR priority

    Topology used:
      b1_____
             |
             |
      s1-----f1-----i1-----l1----r1
             |
       ______|
      b2

      b1 - BSR 1
      b2 - BSR 2
      s1 - Source
      f1 - FHR
      i1 - Intermediate Router (also RP)
      r1 - Receiver

    """

    tgen = get_topogen()
    tc_name = request.node.name
    write_test_header(tc_name)

    app_helper.stop_all_hosts()
    clear_mroute(tgen)
    reset_config_on_routers(tgen)
    clear_pim_interface_traffic(tgen, topo)

    # Don"t run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b1", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b2", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    GROUP_ADDRESS = "225.1.1.1"

    # Use scapy to send pre-defined packet from senser to receiver
    step("Send BSR packet from b1 to FHR")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet3")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    bsr_ip1 = topo["routers"]["b1"]["bsm"]["bsr_packets"]["packet1"]["bsr"].split("/")[
        0
    ]
    time.sleep(1)

    result = app_helper.run_join("r1", GROUP_ADDRESS, "l1")
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify bsr state in FHR
    step("Verify if b1 is chosen as bsr in f1")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip1)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    dut = "l1"
    group = "225.1.1.0/24"
    # Find the elected rp from bsrp-info
    step("Find the elected rp from bsrp-info in LHR in l1")
    rp = find_rp_from_bsrp_info(tgen, dut, bsr_ip1, group)
    assert rp is not {}, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Check RP detail in LHR
    step("Verify RP in LHR l1")
    result = verify_pim_grp_rp_source(tgen, topo, dut, group, "BSR", rp[group])
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Send BSR packet from b2 with same priority
    step("Send BSR packet from b2 to FHR with same priority")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b2", "f1", "packet1")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    bsr_ip2 = topo["routers"]["b2"]["bsm"]["bsr_packets"]["packet2"]["bsr"].split("/")[
        0
    ]
    time.sleep(1)

    logger.info("BSR b1:" + bsr_ip1 + " BSR b2:" + bsr_ip2)
    # Verify bsr state in FHR
    step("Verify if b2 is not chosen as bsr in f1")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip2, expected=False)
    assert result is not True, (
        "Testcase {} : Failed \n "
        "Expected: [{}]: b2 should not be chosen as bsr \n "
        "Found: {}".format(tc_name, "f1", result)
    )

    # Verify if b1 is still chosen as bsr
    step("Verify if b1 is still chosen as bsr in f1")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip1)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Verify if that rp is installed
    step("Verify that same RP in istalled in LHR l1")
    result = verify_pim_grp_rp_source(tgen, topo, dut, group, "BSR", rp[group])
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    step("clear  BSM database before moving to next case")
    clear_bsrp_data(tgen, topo)

    write_test_footer(tc_name)


def test_RP_hash_p0(request):
    """
    Verify RP is selected based on hash function, when BSR advertising the group
    to RP mapping with same priority

    Topology used:
      b1_____
             |
             |
      s1-----f1-----i1-----l1----r1
             |
       ______|
      b2

      b1 - BSR 1
      b2 - BSR 2
      s1 - Source
      f1 - FHR
      i1 - Intermediate Router (also RP)
      r1 - Receiver

    """

    tgen = get_topogen()
    tc_name = request.node.name
    write_test_header(tc_name)

    # Don"t run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    app_helper.stop_all_hosts()
    clear_mroute(tgen)
    reset_config_on_routers(tgen)
    clear_pim_interface_traffic(tgen, topo)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b1", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b2", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    GROUP_ADDRESS = "225.1.1.1"

    # Use scapy to send pre-defined packet from senser to receiver
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet7")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    bsr_ip = topo["routers"]["b1"]["bsm"]["bsr_packets"]["packet1"]["bsr"].split("/")[0]
    time.sleep(1)

    result = app_helper.run_join("r1", GROUP_ADDRESS, "l1")
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    dut = "l1"

    # Verify bsr state in FHR
    step("Verify if b1 chosen as BSR in f1")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    group = "225.1.1.0/24"

    # Find the elected rp from bsrp-info
    step("Find the elected rp from bsrp-info in LHR l1")
    rp = find_rp_from_bsrp_info(tgen, dut, bsr_ip, group)
    assert rp is not {}, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Verify if RP with highest hash value is chosen
    step("Verify if RP(2.2.2.2) with highest hash value is chosen in l1")
    if rp[group] == "2.2.2.2":
        result = True
    else:
        result = "rp expected: 2.2.2.2 got:" + rp[group]

    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Check RP detail in LHR
    step("Verify RP in LHR")
    result = verify_pim_grp_rp_source(tgen, topo, dut, group, "BSR", rp[group])
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    step("clear  BSM database before moving to next case")
    clear_bsrp_data(tgen, topo)

    write_test_footer(tc_name)


def test_BSM_fragmentation_p1(request):
    """
    Verify fragmentation of bootstrap message

    Topology used:
      b1_____
             |
             |
      s1-----f1-----i1-----l1----r1
             |
       ______|
      b2

      b1 - BSR 1
      b2 - BSR 2
      s1 - Source
      f1 - FHR
      i1 - Intermediate Router (also RP)
      r1 - Receiver

    """

    tgen = get_topogen()
    tc_name = request.node.name
    write_test_header(tc_name)

    # Don"t run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    app_helper.stop_all_hosts()
    clear_mroute(tgen)
    reset_config_on_routers(tgen)
    clear_pim_interface_traffic(tgen, topo)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b1", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b2", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    GROUP_ADDRESS = "225.1.1.1"

    bsr_ip = topo["routers"]["b1"]["bsm"]["bsr_packets"]["packet1"]["bsr"].split("/")[0]

    step("Send BSM and verify if all routers have same bsrp before fragment")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet1")
    # Verify bsr state in FHR
    step("Verify if b1 chosen as BSR in f1")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    fhr_node = tgen.routers()["f1"]
    inter_node = tgen.routers()["i1"]
    lhr_node = tgen.routers()["l1"]

    # Verify if bsrp list is same across f1, i1 and l1
    step("Verify if bsrp list is same across f1, i1 and l1")
    bsrp_f1 = fhr_node.vtysh_cmd("show ip pim bsrp-info json", isjson=True)
    logger.info("show_ip_pim_bsrp_info_json f1: \n %s", bsrp_f1)
    bsrp_i1 = inter_node.vtysh_cmd("show ip pim bsrp-info json", isjson=True)
    logger.info("show_ip_pim_bsrp_info_json i1: \n %s", bsrp_i1)
    bsrp_l1 = lhr_node.vtysh_cmd("show ip pim bsrp-info json", isjson=True)
    logger.info("show_ip_pim_bsrp_info_json l1: \n %s", bsrp_l1)

    if bsrp_f1 == bsrp_l1:
        result = True
    else:
        result = "bsrp info in f1 is not same in l1"

    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # set mtu of fhr(f1) to i1 interface to 100 so that bsm fragments
    step("set mtu of fhr(f1) to i1 interface to 100 so that bsm fragments")
    fhr_node.run("ip link set f1-i1-eth2 mtu 100")
    inter_node.run("ip link set i1-f1-eth0 mtu 100")

    # Use scapy to send pre-defined packet from senser to receiver
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet2")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    result = app_helper.run_join("r1", GROUP_ADDRESS, "l1")
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    # Verify bsr state in FHR
    step("Verify if b1 chosen as BSR")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    # Verify if bsrp list is same across f1, i1 and l1
    step("Verify if bsrp list is same across f1, i1 and l1 after fragmentation")
    bsrp_f1 = fhr_node.vtysh_cmd("show ip pim bsrp-info json", isjson=True)
    logger.info("show_ip_pim_bsrp_info_json f1: \n %s", bsrp_f1)
    bsrp_i1 = inter_node.vtysh_cmd("show ip pim bsrp-info json", isjson=True)
    logger.info("show_ip_pim_bsrp_info_json i1: \n %s", bsrp_i1)
    bsrp_l1 = lhr_node.vtysh_cmd("show ip pim bsrp-info json", isjson=True)
    logger.info("show_ip_pim_bsrp_info_json l1: \n %s", bsrp_l1)

    if bsrp_f1 == bsrp_l1:
        result = True
    else:
        result = "bsrp info in f1 is not same in l1"

    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    step("clear  BSM database before moving to next case")
    clear_bsrp_data(tgen, topo)

    write_test_footer(tc_name)


def test_RP_with_all_ip_octet_p1(request):
    """
    Verify when candidate RP advertised with 32 mask length
     and contain all the contacts

    Topology used:
      b1_____
             |
             |
      s1-----f1-----i1-----l1----r1
             |
       ______|
      b2

      b1 - BSR 1
      b2 - BSR 2
      s1 - Source
      f1 - FHR
      i1 - Intermediate Router (also RP)
      r1 - Receiver

    """

    tgen = get_topogen()
    tc_name = request.node.name
    write_test_header(tc_name)

    # Don"t run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    app_helper.stop_all_hosts()
    clear_mroute(tgen)
    reset_config_on_routers(tgen)
    clear_pim_interface_traffic(tgen, topo)

    step("pre-configure BSM packet")
    result = pre_config_to_bsm(
        tgen, topo, tc_name, "b1", "s1", "r1", "f1", "i1", "l1", "packet1"
    )
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    step("Send the IGMP group (225.100.100.100) from receiver connected to FRR")
    GROUP_ADDRESS = "225.200.100.100"

    # Use scapy to send pre-defined packet from senser to receiver
    step("Configure cisco-1 as BSR1")
    result = scapy_send_bsr_raw_packet(tgen, topo, "b1", "f1", "packet8")
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    bsr_ip = topo["routers"]["b1"]["bsm"]["bsr_packets"]["packet8"]["bsr"].split("/")[0]
    time.sleep(1)

    result = app_helper.run_join("r1", GROUP_ADDRESS, "l1")
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    dut = "l1"
    step(
        "Groups are shown with candidate RP with correct mask length 'show ip pim bsrp-info'"
    )
    step("Verify if b1 chosen as BSR in f1")
    result = verify_pim_bsr(tgen, topo, "f1", bsr_ip)
    assert result is True, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    group = topo["routers"]["b1"]["bsm"]["bsr_packets"]["packet9"]["group"]
    step("Find the elected rp from bsrp-info in LHR l1")
    rp = find_rp_from_bsrp_info(tgen, dut, bsr_ip, group)
    assert rp is not {}, "Testcase {} :Failed \n Error {}".format(tc_name, result)

    step("Verify RP in LHR")
    result = verify_pim_grp_rp_source(tgen, topo, dut, group, "BSR", rp[group])
    assert result is True, "Testcase {}:Failed \n Error: {}".format(tc_name, result)

    step("clear  BSM database before moving to next case")
    clear_bsrp_data(tgen, topo)

    write_test_footer(tc_name)


if __name__ == "__main__":
    args = ["-s"] + sys.argv[1:]
    sys.exit(pytest.main(args))