summaryrefslogtreecommitdiff
path: root/tests/TestSuite_sriov_kvm.py
blob: cd3d9c342b366de8ad9a5fd8f87ef418b02f38b5 (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
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
# <COPYRIGHT_TAG>

"""
DPDK Test suite.


Test userland 10Gb PMD.

"""

import re
import pdb
import time

import dts
from qemu_kvm import QEMUKvm
from test_case import TestCase

from pmd_output import PmdOutput

FRAME_SIZE_64 = 64
VM_CORES_MASK = 'all'


class TestSriovKvm(TestCase):

    def set_up_all(self):
        # port_mirror_ref = {port_id: rule_id_list}
        # rule_id should be integer, and should be increased based on
        # the most rule_id when add a rule for a port successfully,
        # case should not be operate it directly
        # example:
        #          port_mirror_ref = {0: 1, 1: 3}
        self.port_mirror_ref = {}
        self.dut_ports = self.dut.get_ports(self.nic)
        self.verify(len(self.dut_ports) >= 1, "Insufficient ports")

        self.vm0 = None
        self.vm1 = None
        self.vm2 = None
        self.vm3 = None

    def set_up(self):
        self.setup_2vm_2pf_env_flag = 0

        self.setup_2vm_2vf_env_flag = 0
        self.setup_2vm_prerequisite_flag = 0

        self.setup_4vm_4vf_env_flag = 0
        self.setup_4vm_prerequisite_flag = 0

    def get_stats(self, dut, portid, rx_tx):
        """
        Get packets number from port statistic
        """

        stats = dut.testpmd.get_pmd_stats(portid)

        if rx_tx == "rx":
            stats_result = [
                stats['RX-packets'], stats['RX-missed'], stats['RX-bytes']]
        elif rx_tx == "tx":
            stats_result = [
                stats['TX-packets'], stats['TX-errors'], stats['TX-bytes']]
        else:
            return None

        return stats_result

    def parse_ether_ip(self, dut, dut_ports, dest_port, **ether_ip):
        """
        dut: which you want to send packet to
        dest_port: the port num must be the index of dut.get_ports()
        ether_ip:
            'ether':
                {
                    'dest_mac':False
                    'src_mac':"52:00:00:00:00:00"
                }
            'dot1q':
                {
                    'vlan':1
                }
            'ip':
                {
                    'dest_ip':"10.239.129.88"
                    'src_ip':"10.239.129.65"
                }
            'udp':
                {
                    'dest_port':53
                    'src_port':53
                }
        """
        ret_ether_ip = {}
        ether = {}
        dot1q = {}
        ip = {}
        udp = {}

        try:
            dut_dest_port = dut_ports[dest_port]
        except Exception as e:
            print e

        tester_port = dut.ports_map[dut_dest_port]
        if not ether_ip.get('ether'):
            ether['dest_mac'] = dut.get_mac_address(dut_dest_port)
            ether['src_mac'] = dut.tester.get_mac(tester_port)
        else:
            if not ether_ip['ether'].get('dest_mac'):
                ether['dest_mac'] = dut.get_mac_address(dut_dest_port)
            else:
                ether['dest_mac'] = ether_ip['ether']['dest_mac']
            if not ether_ip['ether'].get('src_mac'):
                ether['src_mac'] = dut.tester.get_mac(tester_port)
            else:
                ether['src_mac'] = ether_ip["ether"]["src_mac"]

        if not ether_ip.get('dot1q'):
            pass
        else:
            if not ether_ip['dot1q'].get('vlan'):
                dot1q['vlan'] = '1'
            else:
                dot1q['vlan'] = ether_ip['dot1q']['vlan']

        if not ether_ip.get('ip'):
            ip['dest_ip'] = "10.239.129.88"
            ip['src_ip'] = "10.239.129.65"
        else:
            if not ether_ip['ip'].get('dest_ip'):
                ip['dest_ip'] = "10.239.129.88"
            else:
                ip['dest_ip'] = ether_ip['ip']['dest_ip']
            if not ether_ip['ip'].get('src_ip'):
                ip['src_ip'] = "10.239.129.65"
            else:
                ip['src_ip'] = ether_ip['ip']['src_ip']

        if not ether_ip.get('udp'):
            udp['dest_port'] = 53
            udp['src_port'] = 53
        else:
            if not ether_ip['udp'].get('dest_port'):
                udp['dest_port'] = 53
            else:
                udp['dest_port'] = ether_ip['udp']['dest_port']
            if not ether_ip['udp'].get('src_port'):
                udp['src_port'] = 53
            else:
                udp['src_port'] = ether_ip['udp']['src_port']

        ret_ether_ip['ether'] = ether
        ret_ether_ip['dot1q'] = dot1q
        ret_ether_ip['ip'] = ip
        ret_ether_ip['udp'] = udp

        return ret_ether_ip

    def send_packet(self,
                    dut,
                    dut_ports,
                    dest_port,
                    src_port=False,
                    frame_size=FRAME_SIZE_64,
                    count=1,
                    invert_verify=False,
                    **ether_ip):
        """
        Send count packet to portid
        dut: which you want to send packet to
        dest_port: the port num must be the index of dut.get_ports()
        count: 1 or 2 or 3 or ... or 'MANY'
               if count is 'MANY', then set count=1000,
               send packets during 5 seconds.
        ether_ip:
            'ether':
                {
                    'dest_mac':False
                    'src_mac':"52:00:00:00:00:00"
                }
            'dot1q':
                {
                    'vlan':1
                }
            'ip':
                {
                    'dest_ip':"10.239.129.88"
                    'src_ip':"10.239.129.65"
                }
            'udp':
                {
                    'dest_port':53
                    'src_port':53
                }
        """
        during = 0
        loop = 0
        try:
            count = int(count)
        except ValueError as e:
            if count == 'MANY':
                during = 20
                count = 1000 * 10
            else:
                raise e

        gp0rx_pkts, gp0rx_err, gp0rx_bytes = [int(_)
                                              for _ in self.get_stats(dut, dest_port, "rx")]
        if not src_port:
            itf = self.tester.get_interface(
                dut.ports_map[dut_ports[dest_port]])
        else:
            itf = src_port

        ret_ether_ip = self.parse_ether_ip(
            dut,
            dut_ports,
            dest_port,
            **ether_ip)

        pktlen = frame_size - 18
        padding = pktlen - 20

        start = time.time()
        while True:
            self.tester.scapy_foreground()
            self.tester.scapy_append(
                'nutmac="%s"' % ret_ether_ip['ether']['dest_mac'])
            self.tester.scapy_append(
                'srcmac="%s"' % ret_ether_ip['ether']['src_mac'])

            if ether_ip.get('dot1q'):
                self.tester.scapy_append(
                    'vlanvalue=%d' % int(ret_ether_ip['dot1q']['vlan']))
            self.tester.scapy_append(
                'destip="%s"' % ret_ether_ip['ip']['dest_ip'])
            self.tester.scapy_append(
                'srcip="%s"' % ret_ether_ip['ip']['src_ip'])
            self.tester.scapy_append(
                'destport=%d' % ret_ether_ip['udp']['dest_port'])
            self.tester.scapy_append(
                'srcport=%d' % ret_ether_ip['udp']['src_port'])
            if not ret_ether_ip.get('dot1q'):
                send_cmd = 'sendp([Ether(dst=nutmac, src=srcmac)/' + \
                    'IP(dst=destip, src=srcip, len=%s)/' % pktlen + \
                    'UDP(sport=srcport, dport=destport)/' + \
                    'Raw(load="\x50"*%s)], ' % padding + \
                    'iface="%s", count=%d)' % (itf, count)
            else:
                send_cmd = 'sendp([Ether(dst=nutmac, src=srcmac)/Dot1Q(vlan=vlanvalue)/' + \
                           'IP(dst=destip, src=srcip, len=%s)/' % pktlen + \
                           'UDP(sport=srcport, dport=destport)/' + \
                           'Raw(load="\x50"*%s)], iface="%s", count=%d)' % (
                               padding, itf, count)
            self.tester.scapy_append(send_cmd)

            self.tester.scapy_execute()
            loop += 1

            now = time.time()
            if (now - start) >= during:
                break
        time.sleep(.5)

        p0rx_pkts, p0rx_err, p0rx_bytes = [int(_)
                                           for _ in self.get_stats(dut, dest_port, "rx")]

        p0rx_pkts -= gp0rx_pkts
        p0rx_bytes -= gp0rx_bytes

        if not invert_verify:
            self.verify(p0rx_pkts >= count * loop,
                        "Data not received by port")
        else:
            self.verify(p0rx_pkts == 0 or
                        p0rx_pkts < count * loop,
                        "Data received by port, but should not.")
        return count * loop

    def setup_2vm_2pf_env(self):
        p0 = self.dut_ports[0]
        p1 = self.dut_ports[1]

        self.port0 = self.dut.ports_info[p0]['port']
        self.port0.unbind_driver()
        self.port0_pci = self.dut.ports_info[p0]['pci']

        self.port1 = self.dut.ports_info[p1]['port']
        self.port1.unbind_driver()
        self.port1_pci = self.dut.ports_info[p1]['pci']

        vf0_prop = {'prop_host': self.port0_pci}
        vf1_prop = {'prop_host': self.port1_pci}

        # set up VM0 ENV
        self.vm0 = QEMUKvm(self.dut, 'vm0', 'sriov_kvm')
        self.vm0.set_vm_device(driver='pci-assign', **vf0_prop)
        self.vm_dut_0 = self.vm0.start()

        # set up VM1 ENV
        self.vm1 = QEMUKvm(self.dut, 'vm1', 'sriov_kvm')
        self.vm1.set_vm_device(driver='pci-assign', **vf1_prop)
        self.vm_dut_1 = self.vm1.start()

        self.setup_2vm_2vf_env_flag = 1

    def destroy_2vm_2pf_env(self):
        self.vm0.stop()
        self.port0.bind_driver('igb_uio')
        self.vm0 = None

        self.vm1.stop()
        self.port1.bind_driver('igb_uio')
        self.vm1 = None

        self.setup_2vm_2vf_env_flag = 0

    def setup_2vm_2vf_env(self, driver='igb_uio'):
        self.used_dut_port = self.dut_ports[0]

        self.dut.generate_sriov_vfs_by_port(
            self.used_dut_port, 2, driver=driver)
        self.sriov_vfs_port = self.dut.ports_info[
            self.used_dut_port]['vfs_port']

        try:

            for port in self.sriov_vfs_port:
                port.bind_driver('pci-stub')

            time.sleep(1)

            vf0_prop = {'prop_host': self.sriov_vfs_port[0].pci}
            vf1_prop = {'prop_host': self.sriov_vfs_port[1].pci}

            for port_id in self.dut_ports:
                if port_id == self.used_dut_port:
                    continue
                port = self.dut.ports_info[port_id]['port']
                port.bind_driver()

            if driver == 'igb_uio':
                # start testpmd with the two VFs on the host
                self.host_testpmd = PmdOutput(self.dut)
                eal_param = '-b %(vf0)s -b %(vf1)s' % {'vf0': self.sriov_vfs_port[0].pci,
                                                       'vf1': self.sriov_vfs_port[1].pci}
                self.host_testpmd.start_testpmd(
                    "1S/2C/2T", eal_param=eal_param)

            # set up VM0 ENV
            self.vm0 = QEMUKvm(self.dut, 'vm0', 'sriov_kvm')
            self.vm0.set_vm_device(driver='pci-assign', **vf0_prop)
            self.vm_dut_0 = self.vm0.start()
            if self.vm_dut_0 is None:
                raise Exception("Set up VM0 ENV failed!")

            # set up VM1 ENV
            self.vm1 = QEMUKvm(self.dut, 'vm1', 'sriov_kvm')
            self.vm1.set_vm_device(driver='pci-assign', **vf1_prop)
            self.vm_dut_1 = self.vm1.start()
            if self.vm_dut_1 is None:
                raise Exception("Set up VM1 ENV failed!")

            self.setup_2vm_2vf_env_flag = 1
        except Exception as e:
            self.destroy_2vm_2vf_env()
            raise Exception(e)

    def destroy_2vm_2vf_env(self):
        if getattr(self, 'vm0', None):
            self.vm0.stop()
            self.vm0 = None

        if getattr(self, 'vm1', None):
            self.vm1.stop()
            self.vm1 = None

        if getattr(self, 'host_testpmd', None):
            self.host_testpmd.execute_cmd('quit', '# ')
            self.host_testpmd = None

        if getattr(self, 'used_dut_port', None):
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port)
            port = self.dut.ports_info[self.used_dut_port]['port']
            port.bind_driver('igb_uio')
            self.used_dut_port = None

        for port_id in self.dut_ports:
            port = self.dut.ports_info[port_id]['port']
            port.bind_driver('igb_uio')

        self.setup_2vm_2vf_env_flag = 0

    def setup_4vm_4vf_env(self, driver='igb_uio'):
        self.used_dut_port = self.dut_ports[0]

        self.dut.generate_sriov_vfs_by_port(
            self.used_dut_port, 4, driver=driver)
        self.sriov_vfs_port = self.dut.ports_info[self.used_dut_port]['port']

        try:
            for port in self.sriov_vfs_port:
                port.bind_driver('pci-stub')

            time.sleep(1)

            vf0_prop = {'prop_host': self.sriov_vfs_port[0].pci}
            vf1_prop = {'prop_host': self.sriov_vfs_port[1].pci}
            vf2_prop = {'prop_host': self.sriov_vfs_port[2].pci}
            vf3_prop = {'prop_host': self.sriov_vfs_port[3].pci}

            for port_id in self.dut_ports:
                if port_id == self.used_dut_port:
                    continue
                port = self.dut.ports_info[port_id]['port']
                port.bind_driver()

            if driver == 'igb_uio':
                # start testpmd with the four VFs on the host
                self.host_testpmd = PmdOutput(self.dut)
                eal_param = '-b %(vf0) -b %(vf1)s -b %(vf2)s -b %(vf3)s' % \
                    {'vf0': self.sriov_vfs_pci[0],
                     'vf1': self.sriov_vfs_pci[1],
                     'vf2': self.sriov_vfs_pci[2],
                     'vf3': self.sriov_vfs_pci[3]}
                self.host_testpmd.start_testpmd(
                    "1S/2C/2T", eal_param=eal_param)

            self.vm0 = QEMUKvm(self.dut, 'vm0', 'sriov_kvm')
            self.vm0.set_vm_device(driver='pci-assign', **vf0_prop)
            self.vm_dut_0 = self.vm0.start()
            if self.vm_dut_0 is None:
                raise Exception("Set up VM0 ENV failed!")

            self.vm1 = QEMUKvm(self.dut, 'vm1', 'sriov_kvm')
            self.vm1.set_vm_device(driver='pci-assign', **vf1_prop)
            self.vm_dut_1 = self.vm1.start()
            if self.vm_dut_1 is None:
                raise Exception("Set up VM1 ENV failed!")

            self.vm2 = QEMUKvm(self.dut, 'vm2', 'sriov_kvm')
            self.vm2.set_vm_device(driver='pci-assign', **vf2_prop)
            self.vm_dut_2 = self.vm1.start()
            if self.vm_dut_2 is None:
                raise Exception("Set up VM2 ENV failed!")

            self.vm3 = QEMUKvm(self.dut, 'vm3', 'sriov_kvm')
            self.vm3.set_vm_device(driver='pci-assign', **vf3_prop)
            self.vm_dut_3 = self.vm3.start()
            if self.vm_dut_3 is None:
                raise Exception("Set up VM3 ENV failed!")

            self.setup_4vm_4vf_env_flag = 1
        except Exception as e:
            self.destroy_4vm_4vf_env()
            raise Exception(e)

    def destroy_4vm_4vf_env(self):
        if getattr(self, 'vm0', None):
            self.vm0.stop()
            self.vm0 = None

        if getattr(self, 'vm1', None):
            self.vm1.stop()
            self.vm1 = None

        if getattr(self, 'vm2', None):
            self.vm2.stop()
            self.vm2 = None

        if getattr(slef, 'vm3', None):
            self.vm3.stop()
            self.vm3 = None

        if getattr(self, 'host_testpmd', None):
            self.host_testpmd.execute_cmd('stop')
            self.host_testpmd.execute_cmd('quit', '# ')
            self.host_testpmd = None

        if getattr(self, 'used_dut_port', None):
            self.dut.destroy_sriov_vfs_by_port(self.used_dut_port)
            port = self.ports_info[self.used_dut_port]['port']
            port.bind_driver('igb_uio')
            slef.used_dut_port = None

        for port_id in self.dut_ports:
            port = self.dut.ports_info[port_id]['port']
            port.bind_driver('igb_uio')

        self.setup_4vm_4vf_env_flag = 0

    def transform_integer(self, value):
        try:
            value = int(value)
        except ValueError as e:
            raise Exception("Value not integer,but is " + type(value))
        return value

    def make_port_new_ruleid(self, port):
        port = self.transform_integer(port)
        if port not in self.port_mirror_ref.keys():
            max_rule_id = 0
        else:
            rule_ids = sorted(self.port_mirror_ref[port])
            if rule_ids:
                max_rule_id = rule_ids[-1] + 1
            else:
                max_rule_id = 0
        return max_rule_id

    def add_port_ruleid(self, port, rule_id):
        port = self.transform_integer(port)
        rule_id = self.transform_integer(rule_id)

        if port not in self.port_mirror_ref.keys():
            self.port_mirror_ref[port] = [rule_id]
        else:
            self.verify(rule_id not in self.port_mirror_ref[port],
                        "Rule id [%d] has been repeated, please check!" % rule_id)
            self.port_mirror_ref[port].append(rule_id)

    def remove_port_ruleid(self, port, rule_id):
        port = self.transform_integer(port)
        rule_id = self.transform_integer(rule_id)
        if port not in self.port_mirror_ref.keys():
            pass
        else:
            if rule_id not in self.port_mirror_ref[port]:
                pass
            else:
                self.port_mirror_ref[port].remove(rule_id)
            if not self.port_mirror_ref[port]:
                self.port_mirror_ref.pop(port)

    def set_port_mirror_rule(self, port, mirror_name, rule_detail):
        """
        Set the mirror rule for specified port.
        """
        port = self.transform_integer(port)

        rule_id = self.make_port_new_ruleid(port)

        mirror_rule_cmd = "set port %d mirror-rule %d %s %s" % \
            (port, rule_id, mirror_name, rule_detail)
        out = self.dut.send_expect("%s" % mirror_rule_cmd, "testpmd> ")
        self.verify('Bad arguments' not in out, "Set port %d %s failed!" %
                    (port, mirror_name))

        self.add_port_ruleid(port, rule_id)
        return rule_id

    def set_port_pool_mirror(self, port, pool_mirror_rule):
        """
        Set the pool mirror for specified port.
        """
        return self.set_port_mirror_rule(port, 'pool-mirror-up', pool_mirror_rule)

    def set_port_vlan_mirror(self, port, vlan_mirror_rule):
        """
        Set the vlan mirror for specified port.
        """
        return self.set_port_mirror_rule(port, 'vlan-mirror', vlan_mirror_rule)

    def set_port_uplink_mirror(self, port, uplink_mirror_rule):
        """
        Set the uplink mirror for specified port.
        """
        return self.set_port_mirror_rule(port, 'uplink-mirror', uplink_mirror_rule)

    def set_port_downlink_mirror(self, port, downlink_mirror_rule):
        """
        Set the downlink mirror for specified port.
        """
        return self.set_port_mirror_rule(port, 'downlink-mirror', downlink_mirror_rule)

    def reset_port_mirror_rule(self, port, rule_id):
        """
        Reset the pool mirror for specified port.
        """
        port = self.transform_integer(port)
        rule_id = self.transform_integer(rule_id)

        mirror_rule_cmd = "reset port %d mirror-rule %d" % (port, rule_id)
        out = self.dut.send_expect("%s" % mirror_rule_cmd, "testpmd> ")
        self.verify("Bad arguments" not in out,
                    "Reset port %d mirror rule failed!")

        self.remove_port_ruleid(port, rule_id)

    def reset_port_all_mirror_rule(self, port):
        """
        Reset all mirror rules of specified port.
        """
        port = self.transform_integer(port)

        if port not in self.port_mirror_ref.keys():
            pass
        else:
            for rule_id in self.port_mirror_ref[port]:
                self.reset_port_mirror_rule(port, rule_id)

    def setup_two_vm_common_prerequisite(self):
        self.vm0_dut_ports = self.vm_dut_0.get_ports('any')
        self.vm0_testpmd = PmdOutput(self.vm_dut_0)
        self.vm0_testpmd.start_testpmd(VM_CORES_MASK)
        self.vm0_testpmd.execute_cmd('set fwd rxonly')
        self.vm0_testpmd.execute_cmd('start')

        self.vm1_dut_ports = self.vm_dut_1.get_ports('any')
        self.vm1_testpmd = PmdOutput(self.vm_dut_1)
        self.vm1_testpmd.start_testpmd(VM_CORES_MASK)
        self.vm1_testpmd.execute_cmd('set fwd mac')
        self.vm1_testpmd.execute_cmd('start')

        self.setup_2vm_prerequisite_flag = 1

    def destroy_two_vm_common_prerequisite(self):
        self.vm0_testpmd.execute_cmd('stop')
        self.vm0_testpmd.execute_cmd('quit', '# ')
        self.vm0_testpmd = None
        self.vm0_dut_ports = None

        self.vm1_testpmd.execute_cmd('stop')
        self.vm1_testpmd.execute_cmd('quit', '# ')
        self.vm0_testpmd = None
        self.vm1_dut_ports = None

        self.setup_2vm_prerequisite_flag = 0

    def stop_test_setup_two_vm_pf_env(self):
        self.setup_2vm_2pf_env()

        out = self.vm_dut_0.send_expect("ifconfig", '# ')
        print out
        out = self.vm_dut_0.send_expect("lspci -nn | grep -i eth", '# ')
        print out

        out = self.vm_dut_1.send_expect("ifconfig", '# ')
        print out
        out = self.vm_dut_1.send_expect("lspci -nn | grep -i eth", '# ')
        print out

        self.destroy_2vm_2pf_env()

    def test_two_vms_intervm_communication(self):
        self.setup_2vm_2vf_env()

        self.vm0_dut_ports = self.vm_dut_0.get_ports('any')
        self.vm1_dut_ports = self.vm_dut_1.get_ports('any')
        port_id_0 = 0
        packet_num = 10

        self.vm1_testpmd = PmdOutput(self.vm_dut_1)
        self.vm1_testpmd.start_testpmd(VM_CORES_MASK)
        vf1_mac = self.vm1_testpmd.get_port_mac(port_id_0)
        self.vm1_testpmd.execute_cmd('set fwd mac')
        self.vm1_testpmd.execute_cmd('start')

        self.vm0_testpmd = PmdOutput(self.vm_dut_0)
        self.vm0_testpmd.start_testpmd(
            VM_CORES_MASK, "--eth-peer=0,%s" % vf1_mac)
        vf0_mac = self.vm0_testpmd.get_port_mac(port_id_0)
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')

        self.setup_2vm_prerequisite_flag = 1
        time.sleep(2)

        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        self.send_packet(
            self.vm_dut_0, self.vm0_dut_ports, port_id_0, count=packet_num)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        self.verify(
            vm1_end_stats["TX-packets"] - vm1_start_stats["TX-packets"] == packet_num,
            "VM1 transmit packets failed when sending packets to VM0")

    def calculate_stats(self, start_stats, end_stats):
        ret_stats = {}
        for key in start_stats.keys():
            try:
                start_stats[key] = int(start_stats[key])
                end_stats[key] = int(end_stats[key])
            except TypeError:
                ret_stats[key] = end_stats[key]
                continue
            ret_stats[key] = end_stats[key] - start_stats[key]
        return ret_stats

    def test_two_vms_pool_mirror(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        packet_num = 10

        rule_id = self.set_port_pool_mirror(port_id_0, '0x1 dst-pool 1 on')
        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        self.send_packet(
            self.vm_dut_0, self.vm0_dut_ports, port_id_0, count=packet_num)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm1_ret_stats['RX-packets'] == packet_num and
                    vm1_ret_stats['TX-packets'] == packet_num,
                    "Pool mirror failed between VM0 and VM1!")

        self.reset_port_mirror_rule(port_id_0, rule_id)

    def test_two_vms_uplink_mirror(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        packet_num = 10

        rule_id = self.set_port_uplink_mirror(port_id_0, 'dst-pool 1 on')
        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        self.send_packet(
            self.vm_dut_0, self.vm0_dut_ports, port_id_0, count=packet_num)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm1_ret_stats['RX-packets'] == packet_num and
                    vm1_ret_stats['TX-packets'] == packet_num,
                    "Uplink mirror failed between VM0 and VM1!")

        self.reset_port_mirror_rule(port_id_0, rule_id)

    def test_two_vms_downlink_mirror(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        self.vm0_testpmd.execute_cmd('stop')
        self.vm1_testpmd.execute_cmd('stop')

        port_id_0 = 0

        rule_id = self.set_port_downlink_mirror(port_id_0, 'dst-pool 1 on')

        self.vm1_testpmd.execute_cmd('set fwd rxonly')
        self.vm1_testpmd.execute_cmd('start')
        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        self.vm0_testpmd.execute_cmd('start tx_first')
        vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm0_ret_stats = self.calculate_stats(vm0_start_stats, vm0_end_stats)
        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm1_ret_stats['RX-packets'] == vm0_ret_stats['TX-packets'],
                    "Downlink mirror failed between VM0 and VM1!")

        self.reset_port_mirror_rule(port_id_0, rule_id)

    def test_two_vms_vlan_mirror(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        vlan_id = 0
        vf_mask = '0x1'
        packet_num = 10

        self.host_testpmd.execute_cmd(
            'rx_vlan add %d port %d vf %s' % (vlan_id, port_id_0, vf_mask))
        rule_id = self.set_port_vlan_mirror(port_id_0, '0 dst-pool 1 on')

        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        ether_ip = {}
        ether_ip['dot1q'] = {'vlan': '%d' % vlan_id}
        self.send_packet(
            self.vm_dut_0,
            self.vm0_dut_ports,
            port_id_0,
            count=packet_num,
            **ether_ip)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm1_ret_stats['RX-packets'] == packet_num and
                    vm1_ret_stats['TX-packets'] == packet_num,
                    "Vlan mirror failed between VM0 and VM1!")

        self.reset_port_mirror_rule(port_id_0, rule_id)

    def test_two_vms_vlan_and_pool_mirror(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        vlan_id = 3
        vf_mask = '0x2'
        packet_num = 10

        self.host_testpmd.execute_cmd(
            'rx_vlan add %d port %d vf %s' % (vlan_id, port_id_0, vf_mask))
        self.set_port_pool_mirror(port_id_0, '0x1 dst-pool 1 on')
        self.set_port_vlan_mirror(port_id_0, '%d dst-pool 0 on' % vlan_id)

        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        self.send_packet(
            self.vm_dut_0,
            self.vm0_dut_ports,
            port_id_0,
            count=packet_num)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm1_ret_stats['RX-packets'] == packet_num and
                    vm1_ret_stats['TX-packets'] == packet_num,
                    "Pool mirror failed between VM0 and VM1 when set vlan and pool mirror!")

        vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        ether_ip = {}
        ether_ip['dot1q'] = {'vlan': '%d' % vlan_id}
        self.send_packet(
            self.vm_dut_1,
            self.vm1_dut_ports,
            port_id_0,
            count=10 *
            packet_num,
            **ether_ip)
        vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

        vm0_ret_stats = self.calculate_stats(vm0_start_stats, vm0_end_stats)

        self.verify(vm0_ret_stats['RX-packets'] == 10 * packet_num,
                    "Vlan mirror failed between VM0 and VM1 when set vlan and pool mirror!")

        self.reset_port_all_mirror_rule(port_id_0)

    def test_two_vms_uplink_and_downlink_mirror(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        self.vm0_testpmd.execute_cmd('stop')
        self.vm1_testpmd.execute_cmd('stop')

        port_id_0 = 0
        packet_num = 10

        self.set_port_downlink_mirror(port_id_0, 'dst-pool 1 on')
        self.set_port_uplink_mirror(port_id_0, 'dst-pool 1 on')

        self.vm1_testpmd.execute_cmd('set fwd rxonly')
        self.vm1_testpmd.execute_cmd('start')
        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        self.vm0_testpmd.execute_cmd('start tx_first')
        vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm0_ret_stats = self.calculate_stats(vm0_start_stats, vm0_end_stats)
        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm1_ret_stats['RX-packets'] == vm0_ret_stats['TX-packets'],
                    "Downlink mirror failed between VM0 and VM1 " +
                    "when set uplink and downlink mirror!")

        self.vm0_testpmd.execute_cmd('stop')
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')

        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        self.send_packet(
            self.vm_dut_0,
            self.vm0_dut_ports,
            port_id_0,
            count=packet_num)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm1_ret_stats['RX-packets'] == 2 * packet_num,
                    "Uplink and down link mirror failed between VM0 and VM1 " +
                    "when set uplink and downlink mirror!")

        self.reset_port_all_mirror_rule(port_id_0)

    def test_two_vms_vlan_and_pool_and_uplink_and_downlink(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        self.vm0_testpmd.execute_cmd('stop')
        self.vm1_testpmd.execute_cmd('stop')

        port_id_0 = 0
        vlan_id = 3
        vf_mask = '0x2'
        packet_num = 1

        self.set_port_downlink_mirror(port_id_0, 'dst-pool 1 on')
        self.set_port_uplink_mirror(port_id_0, 'dst-pool 1 on')
        self.host_testpmd.execute_cmd("rx_vlan add %d port %d vf %s" %
                                      (vlan_id, port_id_0, vf_mask))
        self.set_port_vlan_mirror(port_id_0, '%d dst-pool 0 on' % vlan_id)
        self.set_port_pool_mirror(port_id_0, '0x1 dst-pool 1 on')

        self.vm1_testpmd.execute_cmd('set fwd rxonly')
        self.vm1_testpmd.execute_cmd('start')
        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        self.vm0_testpmd.execute_cmd('start tx_first')
        vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm0_ret_stats = self.calculate_stats(vm0_start_stats, vm0_end_stats)
        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm1_ret_stats['RX-packets'] == vm0_ret_stats['TX-packets'],
                    "Downlink mirror failed between VM0 and VM1 " +
                    "when set vlan, pool, uplink and downlink mirror!")

        self.vm0_testpmd.execute_cmd('stop')
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')
        vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        self.send_packet(
            self.vm_dut_0,
            self.vm0_dut_ports,
            port_id_0,
            count=packet_num)
        vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm0_ret_stats = self.calculate_stats(vm0_start_stats, vm0_end_stats)
        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm0_ret_stats['RX-packets'] == packet_num and
                    vm0_ret_stats['TX-packets'] == packet_num and
                    vm1_ret_stats['RX-packets'] == 2 * packet_num,
                    "Uplink and downlink mirror failed between VM0 and VM1 " +
                    "when set vlan, pool, uplink and downlink mirror!")

        self.vm0_testpmd.execute_cmd('stop')
        self.vm0_testpmd.execute_cmd('set fwd mac')
        self.vm0_testpmd.execute_cmd('start')

        ether_ip = {}
        ether_ip['dot1q'] = {'vlan': '%d' % vlan_id}
        vm1_start_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)
        vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        self.send_packet(
            self.vm_dut_1,
            self.vm1_dut_ports,
            port_id_0,
            count=packet_num,
            **ether_ip)
        vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        vm1_end_stats = self.vm1_testpmd.get_pmd_stats(port_id_0)

        vm0_ret_stats = self.calculate_stats(vm0_start_stats, vm0_end_stats)
        vm1_ret_stats = self.calculate_stats(vm1_start_stats, vm1_end_stats)

        self.verify(vm0_ret_stats['RX-packets'] == packet_num and
                    vm0_ret_stats['TX-packets'] == packet_num and
                    vm1_ret_stats['RX-packets'] == 2 * packet_num,
                    "Vlan and downlink mirror failed between VM0 and VM1 " +
                    "when set vlan, pool, uplink and downlink mirror!")

        self.reset_port_all_mirror_rule(port_id_0)

    def test_two_vms_add_multi_exact_mac_on_vf(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        vf_num = 0
        packet_num = 10

        for vf_mac in ["00:11:22:33:44:55", "00:55:44:33:22:11"]:
            self.host_testpmd.execute_cmd("mac_addr add port %d vf %d %s" %
                                          (port_id_0, vf_num, vf_mac))

            vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
            ether_ip = {}
            ether_ip['ether'] = {'dest_mac': '%s' % vf_mac}
            self.send_packet(
                self.vm_dut_0,
                self.vm0_dut_ports,
                port_id_0,
                count=packet_num,
                **ether_ip)
            vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

            vm0_ret_stats = self.calculate_stats(
                vm0_start_stats, vm0_end_stats)

            self.verify(vm0_ret_stats['RX-packets'] == packet_num,
                        "Add exact MAC %s failed btween VF0 and VF1" % vf_mac +
                        "when add multi exact MAC address on VF!")

    def test_two_vms_enalbe_or_disable_one_uta_mac_on_vf(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        vf_mac = "00:11:22:33:44:55"
        packet_num = 10

        self.host_testpmd.execute_cmd('set promisc %d on' % port_id_0)
        self.host_testpmd.execute_cmd(
            'set port %d vf 0 rxmode ROPE on' % port_id_0)
        self.host_testpmd.execute_cmd(
            'set port %d vf 1 rxmode ROPE off' % port_id_0)
        self.host_testpmd.execute_cmd(
            'set port %d uta %s on' % (port_id_0, vf_mac))

        vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        ether_ip = {}
        ether_ip['ether'] = {'dest_mac': '%s' % vf_mac}
        self.send_packet(self.vm_dut_0, self.vm0_dut_ports, port_id_0,
                         count=packet_num, **ether_ip)
        vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

        vm0_ret_stats = self.calculate_stats(vm0_start_stats, vm0_end_stats)

        self.verify(vm0_ret_stats['RX-packets'] == packet_num,
                    "Enable one uta MAC failed between VM0 and VM1 " +
                    "when enable or disable one uta MAC address on VF!")

        self.host_testpmd.execute_cmd('set promisc %d off' % port_id_0)
        self.host_testpmd.execute_cmd(
            'set port %d vf 0 rxmode ROPE off' % port_id_0)

        vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
        ether_ip = {}
        ether_ip['ether'] = {'dest_mac': '%s' % vf_mac}
        self.send_packet(self.vm_dut_0, self.vm0_dut_ports, port_id_0,
                         count=packet_num, invert_verify=True, **ether_ip)
        vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

        vm0_ret_stats = self.calculate_stats(vm0_start_stats, vm0_end_stats)

        self.verify(vm0_ret_stats['RX-packets'] == 0,
                    "Disable one uta MAC failed between VM0 and VM1 " +
                    "when enable or disable one uta MAC address on VF!")

    def test_two_vms_add_multi_uta_mac_on_vf(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        packet_num = 10

        for vf_mac in ["00:55:44:33:22:11", "00:55:44:33:22:66"]:
            self.host_testpmd.execute_cmd("set port %d uta %s on" %
                                          (port_id_0, vf_mac))
            self.host_testpmd.execute_cmd("set port %d uta %s on" %
                                          (port_id_0, vf_mac))

        for vf_mac in ["00:55:44:33:22:11", "00:55:44:33:22:66"]:
            vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
            ether_ip = {}
            ether_ip['ether'] = {'dest_mac': '%s' % vf_mac}
            self.send_packet(self.vm_dut_0, self.vm0_dut_ports,
                             port_id_0, count=packet_num, **ether_ip)
            vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

            vm0_ret_stats = self.calculate_stats(
                vm0_start_stats, vm0_end_stats)

            self.verify(vm0_ret_stats['RX-packets'] == packet_num,
                        "Add MULTI uta MAC %s failed between VM0 and VM1 " % vf_mac +
                        "when add multi uta MAC address on VF!")

    def test_two_vms_add_or_remove_uta_mac_on_vf(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        vf_mac = "00:55:44:33:22:11"
        packet_num = 10

        for switch in ['on', 'off', 'on']:
            self.host_testpmd.execute_cmd("set port %d uta %s %s" %
                                          (port_id_0, vf_mac, switch))

            vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
            ether_ip = {}
            ether_ip['ether'] = {'dest_mac': '%s' % vf_mac}
            if switch == 'on':
                self.send_packet(self.vm_dut_0, self.vm0_dut_ports,
                                 port_id_0, count=packet_num, **ether_ip)
            else:
                self.send_packet(self.vm_dut_0, self.vm0_dut_ports, port_id_0,
                                 count=packet_num, invert_verify=True, **ether_ip)
            vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

            vm0_ret_stats = self.calculate_stats(
                vm0_start_stats, vm0_end_stats)

            if switch == 'on':
                self.verify(vm0_ret_stats['RX-packets'] == packet_num,
                            "Add MULTI uta MAC %s failed between VM0 and VM1 " % vf_mac +
                            "when add or remove multi uta MAC address on VF!")
            else:
                self.verify(vm0_ret_stats['RX-packets'] == 0,
                            "Remove MULTI uta MAC %s failed between VM0 and VM1 " % vf_mac +
                            "when add or remove multi uta MAC address on VF!")

    def test_two_vms_pause_rx_queues(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        packet_num = 10

        for switch in ['on', 'off', 'on']:
            self.host_testpmd.execute_cmd("set port %d vf 0 rx %s" %
                                          (port_id_0, switch))

            vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
            if switch == 'on':
                self.send_packet(self.vm_dut_0, self.vm0_dut_ports,
                                 port_id_0, count=packet_num)
            else:
                self.send_packet(self.vm_dut_0, self.vm0_dut_ports, port_id_0,
                                 count=packet_num, invert_verify=True)
            vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

            vm0_ret_stats = self.calculate_stats(
                vm0_start_stats, vm0_end_stats)

            if switch == 'on':
                self.verify(vm0_ret_stats['RX-packets'] == packet_num,
                            "Enable RX queues failed between VM0 and VM1 " +
                            "when enable or pause RX queues on VF!")
            else:
                self.verify(vm0_ret_stats['RX-packets'] == 0,
                            "Pause RX queues failed between VM0 and VM1 " +
                            "when enable or pause RX queues on VF!")

    def test_two_vms_pause_tx_queuse(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        self.vm0_testpmd.execute_cmd("stop")
        self.vm0_testpmd.execute_cmd("set fwd mac")
        self.vm0_testpmd.execute_cmd("start")

        port_id_0 = 0
        packet_num = 10

        for switch in ['on', 'off', 'on']:
            self.host_testpmd.execute_cmd("set port %d vf 0 tx %s" %
                                          (port_id_0, switch))

            vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
            self.send_packet(
                self.vm_dut_0,
                self.vm0_dut_ports,
                port_id_0,
                count=packet_num)
            vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

            vm0_ret_stats = self.calculate_stats(
                vm0_start_stats, vm0_end_stats)

            if switch == 'on':
                self.verify(vm0_ret_stats['TX-packets'] == packet_num,
                            "Enable TX queues failed between VM0 and VM1 " +
                            "when enable or pause TX queues on VF!")
            else:
                self.verify(vm0_ret_stats['TX-packets'] == 0,
                            "Pause TX queues failed between VM0 and VM1 " +
                            "when enable or pause TX queues on VF!")

    def test_two_vms_prevent_rx_broadcast_on_vf(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        port_id_0 = 0
        vf_mac = "FF:FF:FF:FF:FF:FF"
        packet_num = 10

        for switch in ['on', 'off', 'on']:
            self.host_testpmd.execute_cmd("set port %d vf 0 rxmode BAM %s" %
                                          (port_id_0, switch))

            vm0_start_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)
            ether_ip = {}
            ether_ip['ether'] = {'dest_mac': '%s' % vf_mac}
            if switch == 'on':
                self.send_packet(self.vm_dut_0, self.vm0_dut_ports, port_id_0,
                                 count=packet_num, **ether_ip)
            else:
                self.send_packet(self.vm_dut_0, self.vm0_dut_ports, port_id_0,
                                 count=packet_num, invert_verify=True, **ether_ip)
            vm0_end_stats = self.vm0_testpmd.get_pmd_stats(port_id_0)

            vm0_ret_stats = self.calculate_stats(
                vm0_start_stats, vm0_end_stats)

            if switch == 'on':
                self.verify(vm0_ret_stats['RX-packets'] == packet_num,
                            "Enable RX broadcast failed between VM0 and VM1 " +
                            "when enable or disable RX queues on VF!")
            else:
                self.verify(vm0_ret_stats['RX-packets'] == 0,
                            "Disable RX broadcast failed between VM0 and VM1 " +
                            "when enable or pause TX queues on VF!")

    def test_two_vms_negative_input_commands(self):
        self.setup_2vm_2vf_env()
        self.setup_two_vm_common_prerequisite()

        for command in ["set port 0 vf 65 tx on",
                        "set port 2 vf -1 tx off",
                        "set port 0 vf 0 rx oneee",
                        "set port 0 vf 0 rx offdd",
                        "set port 0 vf 64 rxmode BAM on",
                        "set port 0 vf 64 rxmode BAM off",
                        "set port 0 uta 00:11:22:33:44 on",
                        "set port 7 uta 00:55:44:33:22:11 off",
                        "set port 0 vf 34 rxmode ROPE on",
                        "mac_addr add port 0 vf 65 00:55:44:33:22:11",
                        "mac_addr add port 5 vf 0 00:55:44:88:22:11",
                        "set port 0 mirror-rule 0xf uplink-mirror dst-pool 1 on",
                        "set port 0 mirror-rule 2 vlan-mirror 9 dst-pool 1 on",
                        "set port 0 mirror-rule 0 downlink-mirror 0xf dst-pool 2 off",
                        "reset port 0 mirror-rule 4",
                        "reset port 0xff mirror-rule 0"]:
            output = self.host_testpmd.execute_cmd(command)
            error = False

            for error_regx in [r'Bad', r'bad', r'failed', r'-[0-9]+', r'error', r'Invalid']:
                ret_regx = re.search(error_regx, output)
                if ret_regx and ret_regx.group():
                    error = True
                    break
            self.verify(
                error, "Execute command '%s' successfully, it should be failed!" % command)

    def tear_down(self):
        if self.setup_2vm_prerequisite_flag == 1:
            self.destroy_two_vm_common_prerequisite()
        if self.setup_2vm_2vf_env_flag == 1:
            self.destroy_2vm_2vf_env()

        if self.setup_2vm_2pf_env_flag == 1:
            slef.destroy_2vm_2pf_env()

        if self.setup_4vm_prerequisite_flag == 1:
            self.destroy_four_vm_common_prerequisite()
        if self.setup_4vm_4vf_env_flag == 1:
            self.destroy_4vm_4vf_env()

    def tear_down_all(self):
        if getattr(self, 'vm0', None):
            self.vm0.stop()
        if getattr(self, 'vm1', None):
            self.vm1.stop()
        if getattr(self, 'vm2', None):
            self.vm2.stop()
        if getattr(self, 'vm3', None):
            self.vm3.stop()

        for port_id in self.dut_ports:
            self.dut.destroy_sriov_vfs_by_port(port_id)