summaryrefslogtreecommitdiff
path: root/tests/TestSuite_pmd_bonded.py
blob: fd15c1dd917b4269dbabe2226933b73cdfcb829d (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
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
# BSD LICENSE
#
# Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#   * Neither the name of Intel Corporation nor the names of its
#     contributors may be used to endorse or promote products derived
#     from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
DPDK Test suite.


Test userland 10Gb PMD.

"""

import time
import re
import random
from socket import htons, htonl
from functools import wraps

import dts
from test_case import TestCase
from exception import TimeoutException
from settings import TIMEOUT
from pmd_output import PmdOutput

SOCKET_0 = 0
SOCKET_1 = 1

MODE_ROUND_ROBIN = 0
MODE_ACTIVE_BACKUP = 1
MODE_XOR_BALANCE = 2
MODE_BROADCAST = 3
MODE_LACP = 4
MODE_TLB_BALANCE = 5
MODE_ALB_BALANCE = 6

FRAME_SIZE_64 = 64
FRAME_SIZE_65 = 65
FRAME_SIZE_128 = 128
FRAME_SIZE_256 = 256
FRAME_SIZE_512 = 512
FRAME_SIZE_1024 = 1024
FRAME_SIZE_1280 = 1280
FRAME_SIZE_1518 = 1518

S_MAC_IP_PORT = [('52:00:00:00:00:00', '10.239.129.65', 61),
                 ('52:00:00:00:00:01', '10.239.129.66', 62),
                 ('52:00:00:00:00:02', '10.239.129.67', 63)]

D_MAC_IP_PORT = []
LACP_MESSAGE_SIZE = 128


class TestPmdBonded(TestCase):

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

        out = self.dut.send_expect("show port stats %d" % portid, "testpmd> ")

        if rx_tx == "rx":
            result_scanner = r"RX-packets: ([0-9]+)\s*RX-missed: ([0-9]+)\s*RX-bytes:  ([0-9]+)"
        elif rx_tx == "tx":
            result_scanner = r"TX-packets: ([0-9]+)\s*TX-errors: ([0-9]+)\s*TX-bytes:  ([0-9]+)"
        else:
            return None

        scanner = re.compile(result_scanner, re.DOTALL)
        m = scanner.search(out)

        return m.groups()

    def parse_ether_ip(self, dest_port, **ether_ip):
        """
        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 = self.dut_ports[dest_port]
        except Exception, e:
            dut_dest_port = dest_port

        if not ether_ip.get('ether'):
            ether['dest_mac'] = self.dut.get_mac_address(dut_dest_port)
            ether['src_mac'] = "52:00:00:00:00:00"
        else:
            if not ether_ip['ether'].get('dest_mac'):
                ether['dest_mac'] = self.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'] = "52:00:00:00:00:00"
            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,
                    dest_port,
                    src_port=False,
                    frame_size=FRAME_SIZE_64,
                    count=1,
                    invert_verify=False,
                    **ether_ip):
        """
        Send count packet to portid
        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 = 5
                count = 1000
            else:
                raise e

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

        ret_ether_ip = self.parse_ether_ip(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' % 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'):
                self.tester.scapy_append('sendp([Ether(dst=nutmac, src=srcmac)/IP(dst=destip, src=srcip, len=%s)/\
UDP(sport=srcport, dport=destport)/Raw(load="\x50"*%s)], iface="%s", count=%d)' % (pktlen, padding, itf, count))
            else:
                self.tester.scapy_append('sendp([Ether(dst=nutmac, src=srcmac)/Dot1Q(vlan=vlanvalue)/IP(dst=destip, src=srcip, len=%s)/\
UDP(sport=srcport, dport=destport)/Raw(load="\x50"*%s)], iface="%s", count=%d)' % (pktlen, padding, itf, count))

            self.tester.scapy_execute()
            loop += 1

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

        if not src_port:
            p0rx_pkts, p0rx_err, p0rx_bytes = [int(_) for _ in self.get_stats(self.dut_ports[dest_port], "rx")]
        else:
            p0rx_pkts, p0rx_err, p0rx_bytes = [int(_) for _ in self.get_stats(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:
            global LACP_MESSAGE_SIZE
            self.verify(p0rx_pkts == 0 or
                        p0rx_bytes / p0rx_pkts == LACP_MESSAGE_SIZE,
                        "Data received by port, but should not.")
        return count * loop

    def blank_enter(self):
        """
        Just enter blank for the prompt 'testpmd> '
        """
        time.sleep(2)
        self.dut.send_expect(" ", "testpmd> ")

    def dummy_timeout(func):
        """
        There are multiple threads, so maybe you can`t wait for the 'testpmd>',
        if raising TIMEOUT, we will try to expect one more time.
        """
        @wraps(func)
        def ack_timeout(*args, **kwargs):
            pmd_bond_instance = args[0]
            try:
                return func(*args, **kwargs)
            except TimeoutException:
                return pmd_bond_instance.blank_enter()
        return ack_timeout

    @dummy_timeout
    def __send_expect(self, cmds, expected, timeout=TIMEOUT, alt_session=False):
        """
        Encapsulate private expect function because multiple threads printing issue.
        """
        return self.dut.send_expect(cmds, expected, timeout, alt_session)

    def get_value_from_str(self, key_str, regx_str, string):
        """
        Get some values from the given string by the regular expression.
        """
        pattern = r"(?<=%s)%s" % (key_str, regx_str)
        s = re.compile(pattern)
        res = s.search(string)
        if type(res).__name__ == 'NoneType':
            return ' '
        else:
            return res.group(0)

    def get_detail_from_port_info(self, key_str, regx_str, port):
        """
        Get the detail info from the output of pmd cmd 'show port info <port num>'.
        """
        out = self.dut.send_expect("show port info %d" % port, "testpmd> ")
        find_value = self.get_value_from_str(key_str, regx_str, out)
        return find_value

    def get_port_mac(self, port_id):
        """
        Get the specified port MAC.
        """
        return self.get_detail_from_port_info("MAC address: ", "([0-9A-F]{2}:){5}[0-9A-F]{2}", port_id)

    def get_port_connect_socket(self, port_id):
        """
        Get the socket id which the specified port is connectting with.
        """
        return self.get_detail_from_port_info("Connect to socket: ", "\d+", port_id)

    def get_port_memory_socket(self, port_id):
        """
        Get the socket id which the specified port memory is allocated on.
        """
        return self.get_detail_from_port_info("memory allocation on the socket: ", "\d+", port_id)

    def get_port_link_status(self, port_id):
        """
        Get the specified port link status now.
        """
        return self.get_detail_from_port_info("Link status: ", "\d+", port_id)

    def get_port_link_speed(self, port_id):
        """
        Get the specified port link speed now.
        """
        return self.get_detail_from_port_info("Link speed: ", "\d+", port_id)

    def get_port_link_duplex(self, port_id):
        """
        Get the specified port link mode, duplex or siplex.
        """
        return self.get_detail_from_port_info("Link duplex: ", "\S+", port_id)

    def get_port_promiscuous_mode(self, port_id):
        """
        Get the promiscuous mode of port.
        """
        return self.get_detail_from_port_info("Promiscuous mode: ", "\S+", port_id)

    def get_port_allmulticast_mode(self, port_id):
        """
        Get the allmulticast mode of port.
        """
        return self.get_detail_from_port_info("Allmulticast mode: ", "\S+", port_id)

    def get_port_vlan_offload(self, port_id):
        """
        Function: get the port vlan settting info.
        return value:
            'strip':'on'
            'filter':'on'
            'qinq':'off'
        """
        vlan_info = {}
        vlan_info['strip'] = self.get_detail_from_port_info("strip ", '\S+', port_id)
        vlan_info['filter'] = self.get_detail_from_port_info('filter', '\S+', port_id)
        vlan_info['qinq'] = self.get_detail_from_port_info('qinq\(extend\) ', '\S+', port_id)
        return vlan_info

    def get_info_from_bond_config(self, key_str, regx_str, bond_port):
        """
        Get info by executing the command "show bonding config".
        """
        out = self.dut.send_expect("show bonding config %d" % bond_port, "testpmd> ")
        find_value = self.get_value_from_str(key_str, regx_str, out)
        return find_value

    def get_bond_mode(self, bond_port):
        """
        Get the  mode of the bonding device  which you choose.
        """
        return self.get_info_from_bond_config("Bonding mode: ", "\d*", bond_port)

    def get_bond_balance_policy(self, bond_port):
        """
        Get the balance transmit policy of bonding device.
        """
        return self.get_info_from_bond_config("Balance Xmit Policy: ", "\S+", bond_port)

    def get_bond_slaves(self, bond_port):
        """
        Get all the slaves of the bonding device which you choose.
        """
        try:
            return self.get_info_from_bond_config("Slaves \(\d\): \[", "\d*( \d*)*", bond_port)
        except Exception as e:
            return self.get_info_from_bond_config("Slaves: \[", "\d*( \d*)*", bond_port)

    def get_bond_active_slaves(self, bond_port):
        """
        Get the active slaves of the bonding device which you choose.
        """
        try:
            return self.get_info_from_bond_config("Active Slaves \(\d\): \[", "\d*( \d*)*", bond_port)
        except Exception as e:
            return self.get_info_from_bond_config("Acitve Slaves: \[", "\d*( \d*)*", bond_port)

    def get_bond_primary(self, bond_port):
        """
        Get the primary slave of the bonding device which you choose.
        """
        return self.get_info_from_bond_config("Primary: \[", "\d*", bond_port)

    def launch_app(self, pmd_param=" "):
        """
        Launch the testpmd app with the command parameters.
        """
        self.pmdout.start_testpmd("all", param=pmd_param)

    def create_bonded_device(self, mode=0, socket=0, verify_detail=False):
        """
        Create a bonding device with the parameters you specified.
        """
        out = self.dut.send_expect("create bonded device %d %d" % (mode, socket), "testpmd> ")
        self.verify("Created new bonded device" in out,
                    "Create bonded device on mode [%d] socket [%d] failed" % (mode, socket))
        bond_port = self.get_value_from_str("Created new bonded device eth_bond_testpmd_[\d] on \(port ",
                                            "\d+",
                                            out)
        bond_port = int(bond_port)

        if verify_detail:
            out = self.dut.send_expect("show bonding config %d" % bond_port, "testpmd> ")
            self.verify("Bonding mode: %d" % mode in out,
                        "Bonding mode display error when create bonded device")
            self.verify("Slaves: []" in out,
                        "Slaves display error when create bonded device")
            self.verify("Active Slaves: []" in out,
                        "Active Slaves display error when create bonded device")
            self.verify("Primary: []" not in out,
                        "Primary display error when create bonded device")

            out = self.dut.send_expect("show port info %d" % bond_port, "testpmd> ")
            self.verify("Connect to socket: %d" % socket in out,
                        "Bonding port connect socket error")
            self.verify("Link status: down" in out,
                        "Bonding port default link status error")
            self.verify("Link speed: 0 Mbps" in out,
                        "Bonding port default link speed error")

        return bond_port

    def start_all_ports(self):
        """
        Start all the ports which the testpmd can see.
        """
        self.start_port("all")

    def start_port(self, port):
        """
        Start a port which the testpmd can see.
        """
        self.__send_expect("port start %s" % str(port), "testpmd> ")
        time.sleep(3)

    def add_slave_to_bonding_device(self, bond_port, invert_verify=False, *slave_port):
        """
        Add the ports into the bonding device as slaves.
        """
        if len(slave_port) <= 0:
            dts.RED("No port exist when add slave to bonded device")
        for slave_id in slave_port:
            self.__send_expect("add bonding slave %d %d" % (slave_id, bond_port), "testpmd> ")

            slaves = self.get_info_from_bond_config("Slaves \(\d\): \[", "\d*( \d*)*", bond_port)
            if not invert_verify:
                self.verify(str(slave_id) in slaves,
                            "Add port as bonding slave failed")
            else:
                self.verify(str(slave_id) not in slaves,
                            "Add port as bonding slave successfully,should fail")

    def remove_slave_from_bonding_device(self, bond_port, invert_verify=False, *slave_port):
        """
        Remove the specified slave port from the bonding device.
        """
        if len(slave_port) <= 0:
            dts.RED("No port exist when remove slave from bonded device")
        for slave_id in slave_port:
            self.dut.send_expect("remove bonding slave %d %d" % (int(slave_id), bond_port), "testpmd> ")
            out = self.get_info_from_bond_config("Slaves: \[", "\d*( \d*)*", bond_port)
            if not invert_verify:
                self.verify(str(slave_id) not in out,
                            "Remove slave to fail from bonding device")
            else:
                self.verify(str(slave_id) in out,
                            "Remove slave successfully from bonding device,should be failed")

    def remove_all_slaves(self, bond_port):
        """
        Remove all slaves of specified bound device.
        """
        all_slaves = self.get_bond_slaves(bond_port)
        all_slaves = all_slaves.split()
        if len(all_slaves) == 0:
            pass
        else:
            self.remove_slave_from_bonding_device(bond_port, False, *all_slaves)

    def set_primary_for_bonding_device(self, bond_port, slave_port, invert_verify=False):
        """
        Set the primary slave for the bonding device.
        """
        self.dut.send_expect("set bonding primary %d %d" % (slave_port, bond_port), "testpmd> ")
        out = self.get_info_from_bond_config("Primary: \[", "\d*", bond_port)
        if not invert_verify:
            self.verify(str(slave_port) in out,
                        "Set bonding primary port failed")
        else:
            self.verify(str(slave_port) not in out,
                        "Set bonding primary port successfully,should not success")

    def set_mode_for_bonding_device(self, bond_port, mode):
        """
        Set the mode for the bonding device.
        """
        self.dut.send_expect("set bonding mode %d %d" % (mode, bond_port), "testpmd> ")
        mode_value = self.get_bond_mode(bond_port)
        self.verify(str(mode) in mode_value, "Set bonding mode failed")

    def set_mac_for_bonding_device(self, bond_port, mac):
        """
        Set the MAC for the bonding device.
        """
        self.dut.send_expect("set bonding mac_addr %s %s" % (bond_port, mac), "testpmd> ")
        new_mac = self.get_port_mac(bond_port)
        self.verify(new_mac == mac, "Set bonding mac failed")

    def set_balance_policy_for_bonding_device(self, bond_port, policy):
        """
        Set the balance transmit policy for the bonding device.
        """
        self.dut.send_expect("set bonding balance_xmit_policy %d %s" % (bond_port, policy), "testpmd> ")
        new_policy = self.get_bond_balance_policy(bond_port)
        policy = "BALANCE_XMIT_POLICY_LAYER" + policy.lstrip('l')
        self.verify(new_policy == policy, "Set bonding balance policy failed")

    def send_default_packet_to_slave(self, unbound_port, bond_port, pkt_count=100, **slaves):
        """
        Send packets to the slaves and calculate the slave`s RX packets
        and unbond port TX packets.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** slaves:
        ******* 'active'=[]
        ******* 'inactive'=[]
        """
        pkt_orig = {}
        pkt_now = {}
        temp_count = 0
        summary = 0

        # send to slave ports
        pkt_orig = self.get_all_stats(unbound_port, 'tx', bond_port, **slaves)
        for slave in slaves['active']:
            temp_count = self.send_packet(self.dut_ports[slave], False, FRAME_SIZE_64, pkt_count)
            summary += temp_count
        for slave in slaves['inactive']:
            self.send_packet(self.dut_ports[slave], False, FRAME_SIZE_64, pkt_count, True)
        pkt_now = self.get_all_stats(unbound_port, 'tx', bond_port, **slaves)

        for key in pkt_now:
            for num in [0, 1, 2]:
                pkt_now[key][num] -= pkt_orig[key][num]

        return pkt_now, summary

    def send_customized_packet_to_slave(self, unbound_port, bond_port, *pkt_info, **slaves):
        """
        Send packets to the slaves and calculate the slave`s RX packets
        and unbond port TX packets.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** pkt_info: the first is necessary which will describe the packet,
                      the second is optional which will describe the params of
                      the function send_packet
        *** slaves:
        ******* 'active'=[]
        ******* 'inactive'=[]
        """
        pkt_orig = {}
        pkt_now = {}
        temp_count = 0
        summary = 0

        pkt_info_len = len(pkt_info)
        if pkt_info_len < 1:
            self.verify(False, "At least one members for pkt_info!")

        ether_ip = pkt_info[0]
        if pkt_info_len > 1:
            pkt_size = pkt_info[1].get('frame_size', FRAME_SIZE_64)
            pkt_count = pkt_info[1].get('pkt_count', 1)
            invert_verify = pkt_info[1].get('verify', False)
        else:
            pkt_size = FRAME_SIZE_64
            pkt_count = 1
            invert_verify = False

        # send to slave ports
        pkt_orig = self.get_all_stats(unbound_port, 'tx', bond_port, **slaves)
        for slave in slaves['active']:
            temp_count = self.send_packet(self.dut_ports[slave], False, pkt_size, pkt_count, invert_verify, **ether_ip)
            summary += temp_count
        for slave in slaves['inactive']:
            self.send_packet(self.dut_ports[slave], False, FRAME_SIZE_64, pkt_count, True)
        pkt_now = self.get_all_stats(unbound_port, 'tx', bond_port, **slaves)

        for key in pkt_now:
            for num in [0, 1, 2]:
                pkt_now[key][num] -= pkt_orig[key][num]

        return pkt_now, summary

    def send_customized_packet_to_bond_port(self, dut_unbound_port, dut_bond_port, tester_bond_port, pkt_count=100, **slaves):
        pkt_orig = {}
        pkt_now = {}
        temp_count = 0
        summary = 0

        # send to bond_port
        pkt_orig = self.get_all_stats(dut_unbound_port, 'tx', dut_bond_port, **slaves)

        if len(slaves['active']) != 0:
            invert_verify = False
        else:
            invert_verify = True

        dest_mac = self.get_port_mac(dut_bond_port)

        ether_ip = {}
        ether = {}
        ether['src_mac'] = ''
        ether['dest_mac'] = dest_mac
        ether_ip['ether'] = ether

        global S_MAC_IP_PORT
        source = S_MAC_IP_PORT

        for src_mac, src_ip, src_port in source:
            ether_ip['ether']['src_mac'] = src_mac
            temp_count = self.send_packet(dut_bond_port, tester_bond_port, FRAME_SIZE_64, pkt_count, invert_verify, **ether_ip)
            summary += temp_count
        pkt_now = self.get_all_stats(dut_unbound_port, 'tx', dut_bond_port, **slaves)

        for key in pkt_now:
            for num in [0, 1, 2]:
                pkt_now[key][num] -= pkt_orig[key][num]

        return pkt_now, summary

    def send_default_packet_to_unbound_port(self, unbound_port, bond_port, pkt_count=300, **slaves):
        """
        Send packets to the unbound port and calculate unbound port RX packets
        and the slave`s TX packets.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** slaves:
        ******* 'actvie':[]
        ******* 'inactive':[]
        """
        pkt_orig = {}
        pkt_now = {}
        summary = 0

        # send to unbonded device
        pkt_orig = self.get_all_stats(unbound_port, 'rx', bond_port, **slaves)
        summary = self.send_packet(unbound_port, False, FRAME_SIZE_64, pkt_count)
        pkt_now = self.get_all_stats(unbound_port, 'rx', bond_port, **slaves)

        for key in pkt_now:
            for num in [0, 1, 2]:
                pkt_now[key][num] -= pkt_orig[key][num]

        return pkt_now, summary

    def send_customized_packet_to_unbound_port(self, unbound_port, bond_port, policy, vlan_tag=False, pkt_count=100, **slaves):
        """
        Verify that transmitting the packets correctly in the XOR mode.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** policy:'L2' , 'L23' or 'L34'
        *** vlan_tag:False or True
        *** slaves:
        ******* 'active'=[]
        ******* 'inactive'=[]
        """
        pkt_orig = {}
        pkt_now = {}
        summary = 0
        temp_count = 0

        # send to unbound_port
        pkt_orig = self.get_all_stats(unbound_port, 'rx', bond_port, **slaves)

        dest_mac = self.dut.get_mac_address(self.dut_ports[unbound_port])
        dest_ip = "10.239.129.88"
        dest_port = 53

        global D_MAC_IP_PORT
        D_MAC_IP_PORT = [dest_mac, dest_ip, dest_port]

        ether_ip = {}
        ether = {}
        ip = {}
        udp = {}

        ether['dest_mac'] = False
        ip['dest_ip'] = dest_ip
        udp['dest_port'] = 53
        if vlan_tag:
            dot1q = {}
            dot1q['vlan'] = random.randint(1, 50)
            ether_ip['dot1q'] = dot1q

        ether_ip['ether'] = ether
        ether_ip['ip'] = ip
        ether_ip['udp'] = udp

        global S_MAC_IP_PORT
        source = S_MAC_IP_PORT

        for src_mac, src_ip, src_port in source:
            ether_ip['ether']['src_mac'] = src_mac
            ether_ip['ip']['src_ip'] = src_ip
            ether_ip['udp']['src_port'] = src_port
            temp_count = self.send_packet(unbound_port, False, FRAME_SIZE_64, pkt_count, False, **ether_ip)
            summary += temp_count
        pkt_now = self.get_all_stats(unbound_port, 'rx', bond_port, **slaves)

        for key in pkt_now:
            for num in [0, 1, 2]:
                pkt_now[key][num] -= pkt_orig[key][num]

        return pkt_now, summary

    #
    # Test cases.
    #
    def set_up_all(self):
        """
        Run before each test suite
        """
        self.verify('bsdapp' not in self.target, "Bonding not support freebsd")
        self.frame_sizes = [64, 65, 128, 256, 512, 1024, 1280, 1518]

        self.eth_head_size = 18
        self.ip_head_size = 20
        self.udp_header_size = 8

        self.dut_ports = self.dut.get_ports()

        self.port_mask = dts.create_mask(self.dut_ports)

        self.verify(len(self.dut_ports) >= 4, "Insufficient ports")

        self.ports_socket = self.dut.get_numa_id(self.dut_ports[0])

        self.all_cores_mask = dts.create_mask(self.dut.get_core_list("all"))

        self.pmdout = PmdOutput(self.dut)

        self.tester_bond = "bond0"

    def set_up(self):
        """
        Run before each test case.
        """
        if self.dut.want_func_tests:
            self.launch_app()
        elif self.dut.want_perf_tests:
            pmd_param = "--burst=32 --rxfreet=32 --mbcache=250 --txpt=32 \
--rxht=8 --rxwt=0 --txfreet=32 --txrst=32 --txqflags=0xf01"
            self.launch_app(pmd_param)
        else:
            self.verify(False,
                        "Test type not etting," +
                        "please check framework to set test type to be function or performance.")

    def verify_bound_basic_opt(self, mode_set):
        """
        Do some basic operations to bonded devices and slaves,
        such as adding, removing, setting primary or setting mode.
        """
        bond_port_0 = self.create_bonded_device(mode_set, SOCKET_0, True)
        self.add_slave_to_bonding_device(bond_port_0, False, self.dut_ports[1])

        mode_value = self.get_bond_mode(bond_port_0)
        self.verify('%d' % mode_set in mode_value,
                    "Setting bonding mode error")

        bond_port_1 = self.create_bonded_device(mode_set, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port_0, False, self.dut_ports[0])
        self.add_slave_to_bonding_device(bond_port_1, True, self.dut_ports[0])

        OTHER_MODE = mode_set + 1 if not mode_set else mode_set - 1
        self.set_mode_for_bonding_device(bond_port_0, OTHER_MODE)
        self.set_mode_for_bonding_device(bond_port_0, mode_set)

        self.add_slave_to_bonding_device(bond_port_0, False, self.dut_ports[2])
        time.sleep(5)
        self.set_primary_for_bonding_device(bond_port_0, self.dut_ports[2])

        self.remove_slave_from_bonding_device(bond_port_0, False, self.dut_ports[2])
        primary_now = self.get_bond_primary(bond_port_0)
        self.verify(int(primary_now) == self.dut_ports[1],
                    "Reset primary slave failed after removing primary slave")

        for bond_port in [bond_port_0, bond_port_1]:
            self.remove_all_slaves(bond_port)

        self.dut.send_expect("quit", "# ")
        self.launch_app()

    def verify_bound_mac_opt(self, mode_set):
        """
        Create bonded device, add one slave,
        verify bonded device MAC action varies with the mode.
        """
        mac_address_0_orig = self.get_port_mac(self.dut_ports[0])
        mac_address_1_orig = self.get_port_mac(self.dut_ports[1])
        mac_address_2_orig = self.get_port_mac(self.dut_ports[2])
        mac_address_3_orig = self.get_port_mac(self.dut_ports[3])

        bond_port = self.create_bonded_device(mode_set, SOCKET_1)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[1])

        mac_address_bond_orig = self.get_port_mac(bond_port)
        self.verify(mac_address_1_orig == mac_address_bond_orig,
                    "Bonded device MAC address not same with first slave MAC")

        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[2])
        mac_address_2_now = self.get_port_mac(self.dut_ports[2])
        mac_address_bond_now = self.get_port_mac(bond_port)
        if mode_set in [MODE_ROUND_ROBIN, MODE_XOR_BALANCE, MODE_BROADCAST]:
            self.verify(mac_address_1_orig == mac_address_bond_now and
                        mac_address_bond_now == mac_address_2_now,
                        "NOT all slaves MAC address same with bonding device in mode %d" % mode_set)
        else:
            self.verify(mac_address_1_orig == mac_address_bond_now and
                        mac_address_bond_now != mac_address_2_now,
                        "All slaves should not be the same in mode %d"
                        % mode_set)

        new_mac = "00:11:22:00:33:44"
        self.set_mac_for_bonding_device(bond_port, new_mac)
        self.start_port(bond_port)
        mac_address_1_now = self.get_port_mac(self.dut_ports[1])
        mac_address_2_now = self.get_port_mac(self.dut_ports[2])
        mac_address_bond_now = self.get_port_mac(bond_port)
        if mode_set in [MODE_ROUND_ROBIN, MODE_XOR_BALANCE, MODE_BROADCAST]:
            self.verify(mac_address_1_now == mac_address_2_now == mac_address_bond_now == new_mac,
                        "Set mac failed for bonding device in mode %d" % mode_set)
        elif mode_set == MODE_LACP:
            self.verify(mac_address_bond_now == new_mac and
                        mac_address_1_now != new_mac and
                        mac_address_2_now != new_mac and
                        mac_address_1_now != mac_address_2_now,
                        "Set mac failed for bonding device in mode %d" % mode_set)
        elif mode_set in [MODE_ACTIVE_BACKUP, MODE_TLB_BALANCE]:
            self.verify(mac_address_bond_now == new_mac and
                        mac_address_1_now == new_mac and
                        mac_address_bond_now != mac_address_2_now,
                        "Set mac failed for bonding device in mode %d" % mode_set)

        self.set_primary_for_bonding_device(bond_port, self.dut_ports[2], False)
        mac_address_1_now = self.get_port_mac(self.dut_ports[1])
        mac_address_2_now = self.get_port_mac(self.dut_ports[2])
        mac_address_bond_now = self.get_port_mac(bond_port)
        self.verify(mac_address_bond_now == new_mac,
                    "Slave MAC changed when set primary slave")

        mac_address_1_orig = mac_address_1_now
        self.remove_slave_from_bonding_device(bond_port, False, self.dut_ports[2])
        mac_address_2_now = self.get_port_mac(self.dut_ports[2])
        self.verify(mac_address_2_now == mac_address_2_orig,
                    "MAC not back to original after removing the port")

        mac_address_1_now = self.get_port_mac(self.dut_ports[1])
        mac_address_bond_now = self.get_port_mac(bond_port)
        self.verify(mac_address_bond_now == new_mac and
                    mac_address_1_now == mac_address_1_orig,
                    "Bonding device or slave MAC changed after removing the primary slave")

        self.remove_all_slaves(bond_port)
        self.dut.send_expect("quit", "# ")
        self.launch_app()

    def verify_bound_promisc_opt(self, mode_set):
        """
        Set promiscuous mode on bonded device, verify bonded device and all slaves
        have different actions by the different modes.
        """
        unbound_port = self.dut_ports[3]
        bond_port = self.create_bonded_device(mode_set, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port,
                                         False,
                                         self.dut_ports[0],
                                         self.dut_ports[1],
                                         self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (unbound_port, bond_port), "testpmd> ")
        self.start_port(bond_port)
        self.dut.send_expect("start", "testpmd> ")

        port_disabled_num = 0
        testpmd_all_ports = self.dut_ports
        testpmd_all_ports.append(bond_port)
        for port_id in testpmd_all_ports:
            value = self.get_detail_from_port_info("Promiscuous mode: ", "enabled", port_id)
            if not value:
                port_disabled_num += 1
        self.verify(port_disabled_num == 0,
                    "Not all slaves of bonded device turn promiscuous mode on by default.")

        ether_ip = {}
        ether = {}
        ether['dest_mac'] = "00:11:22:33:44:55"
        ether_ip['ether'] = ether

        send_param = {}
        pkt_count = 1
        send_param['pkt_count'] = pkt_count
        pkt_info = [ether_ip, send_param]

        slaves = {}
        slaves['active'] = [self.dut_ports[0]]
        slaves['inactive'] = []

        pkt_now, summary = self.send_customized_packet_to_slave(unbound_port, bond_port, *pkt_info, **slaves)
        if mode_set == MODE_LACP:
            do_transmit = False
            pkt_size = 0
            if pkt_now[unbound_port][0]:
                do_transmit = True
                pkt_size = pkt_now[unbound_port][2] / pkt_now[unbound_port][0]
            self.verify(do_transmit and pkt_size != LACP_MESSAGE_SIZE,
                        "Data not received by slave or bonding device when promiscuous enabled")
        else:
            self.verify(pkt_now[self.dut_ports[0]][0] == pkt_now[bond_port][0] and
                        pkt_now[bond_port][0] == pkt_count,
                        "Data not received by slave or bonding device when promiscuous enabled")

        self.dut.send_expect("set promisc %s off" % bond_port, "testpmd> ")
        port_disabled_num = 0
        testpmd_all_ports = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2], bond_port]
        for port_id in testpmd_all_ports:
            value = self.get_detail_from_port_info('Promiscuous mode: ', 'disabled', port_id)
            if value == 'disabled':
                port_disabled_num += 1
        if mode_set in [MODE_ROUND_ROBIN, MODE_XOR_BALANCE, MODE_BROADCAST]:
            self.verify(port_disabled_num == 4,
                        "Not all slaves of bonded device turn promiscuous mode off in mode %d." % mode_set)
        elif mode_set == MODE_LACP:
            self.verify(port_disabled_num == 1,
                        "Not only turn bound device promiscuous mode off in mode %d" % mode_set)
        else:
            self.verify(port_disabled_num == 2,
                        "Not only the primary slave turn promiscous mode off in mode %d, " % mode_set +
                        " when bonded device  promiscous disabled.")

        if mode_set != MODE_LACP:
            send_param['verify'] = True
        pkt_now, summary = self.send_customized_packet_to_slave(unbound_port, bond_port, *pkt_info, **slaves)
        if mode_set == MODE_LACP:
            do_transmit = False
            pkt_size = 0
            if pkt_now[unbound_port][0]:
                do_transmit = True
                pkt_size = pkt_now[unbound_port][2] / pkt_now[unbound_port][0]
            self.verify(not do_transmit or
                        pkt_size == LACP_MESSAGE_SIZE,
                        "Data received by slave or bonding device when promiscuous disabled")
        else:
            self.verify(pkt_now[self.dut_ports[0]][0] == 0 and
                        pkt_now[bond_port][0] == 0,
                        "Data received by slave or bonding device when promiscuous disabled")

        pkt_now, summary = self.send_default_packet_to_slave(self.dut_ports[3], bond_port, pkt_count, **slaves)
        if mode_set == MODE_LACP:
            do_transmit = False
            pkt_size = 0
            if pkt_now[unbound_port][0]:
                do_transmit = True
                pkt_size = pkt_now[unbound_port][2] / pkt_now[unbound_port][0]
            self.verify(not do_transmit or
                        pkt_size != LACP_MESSAGE_SIZE,
                        "RX or TX packet number not correct when promiscuous disabled")
        else:
            self.verify(pkt_now[self.dut_ports[0]][0] == pkt_now[bond_port][0] and
                        pkt_now[self.dut_ports[3]][0] == pkt_now[bond_port][0] and
                        pkt_now[bond_port][0] == pkt_count,
                        "RX or TX packet number not correct when promiscuous disabled")

        self.remove_all_slaves(bond_port)
        self.dut.send_expect("quit", "# ")
        self.launch_app()

    def test_bound_basic_opt(self):
        self.verify_bound_basic_opt(MODE_ACTIVE_BACKUP)

    def test_bound_mac_opt(self):
        self.verify_bound_mac_opt(MODE_BROADCAST)

    def test_bound_promisc_opt(self):
        self.verify_bound_promisc_opt(MODE_BROADCAST)

    def admin_tester_port(self, local_port, status):
        """
        Do some operations to the network interface port, such as "up" or "down".
        """
        if self.tester.get_os_type() == 'freebsd':
            self.tester.admin_ports(local_port, status)
        else:
            eth = self.tester.get_interface(local_port)
            self.tester.admin_ports_linux(eth, status)
        time.sleep(5)

    def verify_round_robin_rx(self, unbound_port, bond_port, **slaves):
        """
        Verify the receiving packet are all correct in the round robin mode.
            slaves:
                'active' = []
                'inactive' = []
        """
        pkt_count = 100
        pkt_now = {}
        pkt_now, summary = self.send_default_packet_to_slave(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        self.verify(pkt_now[unbound_port][0] == pkt_count * slaves['active'].__len__(), "Unbonded port has error TX pkt count in mode 0")
        self.verify(pkt_now[bond_port][0] == pkt_count * slaves['active'].__len__(), "Bonding port has error RX pkt count in mode 0")

    def verify_round_robin_tx(self, unbound_port, bond_port, **slaves):
        """
        Verify the transmitting packet are all correct in the round robin mode.
            slaves:
                'active' = []
                'inactive' = []
        """
        pkt_count = 300
        pkt_now = {}
        pkt_now, summary = self.send_default_packet_to_unbound_port(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        if slaves['active'].__len__() == 0:
            self.verify(pkt_now[bond_port][0] == 0, "Bonding port should not have TX pkt in mode 0 when all slaves down")
        else:
            self.verify(pkt_now[bond_port][0] == pkt_count, "Bonding port has error TX pkt count in mode 0")
        for slave in slaves['active']:
            self.verify(pkt_now[slave][0] == pkt_count / slaves['active'].__len__(), "Active slave has error TX pkt count in mode 0")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0, "Inactive slave has error TX pkt count in mode 0")

    def test_round_robin_rx_tx(self):
        """
        Verify that receiving and transmitting the packets correctly in the round robin mode.
        """
        bond_port = self.create_bonded_device(MODE_ROUND_ROBIN, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")

        slaves = {}
        slaves['active'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
        slaves['inactive'] = []
        self.verify_round_robin_rx(self.dut_ports[3], bond_port, **slaves)
        self.verify_round_robin_tx(self.dut_ports[3], bond_port, **slaves)

    def test_round_robin_one_slave_down(self):
        """
        Verify that receiving and transmitting the packets correctly in the round robin mode,
        when bringing any one slave of the bonding device link down.
        """
        bond_port = self.create_bonded_device(MODE_ROUND_ROBIN, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")

        stat = self.tester.get_port_status(self.tester.get_local_port(self.dut_ports[0]))
        self.dut.send_expect("show bonding config %d" % bond_port, "testpmd> ")
        self.dut.send_expect("show port info all", "testpmd> ")

        try:
            slaves = {}
            slaves['active'] = [self.dut_ports[1], self.dut_ports[2]]
            slaves['inactive'] = [self.dut_ports[0]]
            self.verify_round_robin_rx(self.dut_ports[3], bond_port, **slaves)
            self.verify_round_robin_tx(self.dut_ports[3], bond_port, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")

    def test_round_robin_all_slaves_down(self):
        """
        Verify that receiving and transmitting the packets correctly in the round robin mode,
        when bringing all slaves of the bonding device link down.
        """
        bond_port = self.create_bonded_device(MODE_ROUND_ROBIN, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")

        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "down")

        try:
            slaves = {}
            slaves['active'] = []
            slaves['inactive'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
            self.verify_round_robin_rx(self.dut_ports[3], bond_port, **slaves)
            self.verify_round_robin_tx(self.dut_ports[3], bond_port, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "up")

    def get_all_stats(self, unbound_port, rx_tx, bond_port, **slaves):
        """
        Get all the port stats which the testpmd can dicover.
        Parameters:
        *** unbound_port: pmd port id
        *** rx_tx: unbond port stat 'rx' or 'tx'
        *** bond_port: bonding port
        *** slaves:
        ******** 'active' = []
        ******** 'inactive' = []
        """
        pkt_now = {}

        if rx_tx == 'rx':
            bond_stat = 'tx'
        else:
            bond_stat = 'rx'

        pkt_now[unbound_port] = [int(_) for _ in self.get_stats(unbound_port, rx_tx)]
        pkt_now[bond_port] = [int(_) for _ in self.get_stats(bond_port, bond_stat)]
        for slave in slaves['active']:
            pkt_now[slave] = [int(_) for _ in self.get_stats(slave, bond_stat)]
        for slave in slaves['inactive']:
            pkt_now[slave] = [int(_) for _ in self.get_stats(slave, bond_stat)]

        return pkt_now

    def verify_active_backup_rx(self, unbound_port, bond_port, **slaves):
        """
        Verify the RX packets are all correct in the active-backup mode.
        Parameters:
        *** slaves:
        ******* 'active' = []
        ******* 'inactive' = []
        """
        pkt_count = 100
        pkt_now = {}

        slave_num = slaves['active'].__len__()
        if slave_num != 0:
            active_flag = 1
        else:
            active_flag = 0

        pkt_now, summary = self.send_default_packet_to_slave(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        self.verify(pkt_now[bond_port][0] == pkt_count * slave_num, "Not correct RX pkt on bond port in mode 1")
        self.verify(pkt_now[unbound_port][0] == pkt_count * active_flag, "Not correct TX pkt on unbound port in mode 1")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0, "Not correct RX pkt on inactive port in mode 1")
        for slave in slaves['active']:
            self.verify(pkt_now[slave][0] == pkt_count, "Not correct RX pkt on active port in mode 1")

    def verify_active_backup_tx(self, unbound_port, bond_port, **slaves):
        """
        Verify the TX packets are all correct in the active-backup mode.
        Parameters:
        *** slaves:
        ******* 'active' = []
        ******* 'inactive' = []
        """
        pkt_count = 0
        pkt_now = {}

        if slaves['active'].__len__() != 0:
            primary_port = slaves['active'][0]
            active_flag = 1
        else:
            active_flag = 0

        pkt_now, summary = self.send_default_packet_to_unbound_port(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        self.verify(pkt_now[bond_port][0] == pkt_count * active_flag, "Not correct RX pkt on bond port in mode 1")
        if active_flag == 1:
            self.verify(pkt_now[primary_port][0] == pkt_count, "Not correct TX pkt on primary port in mode 1")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0, "Not correct TX pkt on inactive port in mode 1")
        for slave in [slave for slave in slaves['active'] if slave != primary_port]:
            self.verify(pkt_now[slave][0] == 0, "Not correct TX pkt on backup port in mode 1")

    def test_active_backup_rx_tx(self):
        """
        Verify receiving and transmitting the packets correctly in the active-backup mode.
        """
        bond_port = self.create_bonded_device(MODE_ACTIVE_BACKUP, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        time.sleep(5)

        slaves = {}
        slaves['active'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
        slaves['inactive'] = []
        self.verify_active_backup_rx(self.dut_ports[3], bond_port, **slaves)
        self.verify_active_backup_tx(self.dut_ports[3], bond_port, **slaves)

    def test_active_backup_change_primary(self):
        """
        Verify that receiving and transmitting the packets correctly in the active-backup mode,
        when you change the primary slave.
        """
        bond_port = self.create_bonded_device(MODE_ACTIVE_BACKUP, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.set_primary_for_bonding_device(bond_port, self.dut_ports[1])
        time.sleep(5)

        slaves = {}
        slaves['active'] = [self.dut_ports[1], self.dut_ports[0], self.dut_ports[2]]
        slaves['inactive'] = []
        self.verify_active_backup_rx(self.dut_ports[3], bond_port, **slaves)
        self.verify_active_backup_tx(self.dut_ports[3], bond_port, **slaves)

    def test_active_backup_one_slave_down(self):
        """
        Verify that receiving and transmitting the pcakets correctly in the active-backup mode,
        when bringing any one slave of the bonding device link down.
        """
        bond_port = self.create_bonded_device(MODE_ACTIVE_BACKUP, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")
        primary_port = int(self.get_bond_primary(bond_port))

        try:
            slaves = {}
            active_slaves = [self.dut_ports[1], self.dut_ports[2]]
            active_slaves.remove(primary_port)
            slaves['active'] = [primary_port]
            slaves['active'].extend(active_slaves)
            slaves['inactive'] = [self.dut_ports[0]]
            self.verify_active_backup_rx(self.dut_ports[3], bond_port, **slaves)
            self.verify_active_backup_tx(self.dut_ports[3], bond_port, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")

    def test_active_backup_all_slaves_down(self):
        """
        Verify that receiving and transmitting that packets correctly in the active-backup mode,
        when bringing all slaves of the bonding device link down.
        """
        bond_port = self.create_bonded_device(MODE_ACTIVE_BACKUP, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "down")

        try:
            slaves = {}
            slaves['active'] = []
            slaves['inactive'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
            self.verify_active_backup_rx(self.dut_ports[3], bond_port, **slaves)
            self.verify_active_backup_tx(self.dut_ports[3], bond_port, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "up")

    def translate_mac_str_into_int(self, mac_str):
        """
        Translate the MAC type from the string into the int.
        """
        mac_hex = '0x'
        for mac_part in mac_str.split(':'):
            mac_hex += mac_part
        return int(mac_hex, 16)

    def mac_hash(self, dest_mac, src_mac):
        """
        Generate the hash value with the source and destination MAC.
        """
        dest_port_mac = self.translate_mac_str_into_int(dest_mac)
        src_port_mac = self.translate_mac_str_into_int(src_mac)
        src_xor_dest = dest_port_mac ^ src_port_mac
        xor_value_1 = src_xor_dest >> 32
        xor_value_2 = (src_xor_dest >> 16) ^ (xor_value_1 << 16)
        xor_value_3 = src_xor_dest ^ (xor_value_1 << 32) ^ (xor_value_2 << 16)
        return htons(xor_value_1 ^ xor_value_2 ^ xor_value_3)

    def translate_ip_str_into_int(self, ip_str):
        """
        Translate the IP type from the string into the int.
        """
        ip_part_list = ip_str.split('.')
        ip_part_list.reverse()
        num = 0
        ip_int = 0
        for ip_part in ip_part_list:
            ip_part_int = int(ip_part) << (num * 8)
            ip_int += ip_part_int
            num += 1
        return ip_int

    def ipv4_hash(self, dest_ip, src_ip):
        """
        Generate the hash value with the source and destination IP.
        """
        dest_ip_int = self.translate_ip_str_into_int(dest_ip)
        src_ip_int = self.translate_ip_str_into_int(src_ip)
        return htonl(dest_ip_int ^ src_ip_int)

    def udp_hash(self, dest_port, src_port):
        """
        Generate the hash value with the source and destination port.
        """
        return htons(dest_port ^ src_port)

    def policy_and_slave_hash(self, policy, **slaves):
        """
        Generate the hash value by the policy and active slave number.
        *** policy:'L2' , 'L23' or 'L34'
        *** slaves:
        ******* 'active'=[]
        ******* 'inactive'=[]
        """
        global S_MAC_IP_PORT
        source = S_MAC_IP_PORT

        global D_MAC_IP_PORT
        dest_mac = D_MAC_IP_PORT[0]
        dest_ip = D_MAC_IP_PORT[1]
        dest_port = D_MAC_IP_PORT[2]

        hash_values = []
        if len(slaves['active']) != 0:
            for src_mac, src_ip, src_port in source:
                if policy == "L2":
                    hash_value = self.mac_hash(dest_mac, src_mac)
                elif policy == "L23":
                    hash_value = self.mac_hash(dest_mac, src_mac) ^ self.ipv4_hash(dest_ip, src_ip)
                else:
                    hash_value = self.ipv4_hash(dest_ip, src_ip) ^ self.udp_hash(dest_port, src_port)

                if policy in ("L23", "L34"):
                    hash_value ^= hash_value >> 16
                hash_value ^= hash_value >> 8
                hash_value = hash_value % len(slaves['active'])
                hash_values.append(hash_value)

        return hash_values

    def slave_map_hash(self, port, order_ports):
        """
        Find the hash value by the given slave port id.
        """
        if len(order_ports) == 0:
            return None
        else:
            order_ports = order_ports.split()
            return order_ports.index(str(port))

    def verify_xor_rx(self, unbound_port, bond_port, **slaves):
        """
        Verify receiving the pcakets correctly in the XOR mode.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** slaves:
        ******* 'active'=[]
        ******* 'inactive'=[]
        """
        pkt_count = 100
        pkt_now = {}

        pkt_now, summary = self.send_default_packet_to_slave(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        for slave in slaves['active']:
            self.verify(pkt_now[slave][0] == pkt_count, "Slave have error RX packet in XOR")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0, "Slave have error RX packet in XOR")
        self.verify(pkt_now[unbound_port][0] == pkt_count * len(slaves['active']), "Unbonded device have error TX packet in XOR")

    def verify_xor_tx(self, unbound_port, bond_port, policy, vlan_tag=False, **slaves):
        """
        Verify that transmitting the packets correctly in the XOR mode.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** policy:'L2' , 'L23' or 'L34'
        *** vlan_tag:False or True
        *** slaves:
        ******* 'active'=[]
        ******* 'inactive'=[]
        """
        pkt_count = 100
        pkt_now = {}

        pkt_now, summary = self.send_customized_packet_to_unbound_port(unbound_port, bond_port, policy, vlan_tag=False, pkt_count=pkt_count, **slaves)

        hash_values = []
        hash_values = self.policy_and_slave_hash(policy, **slaves)

        order_ports = self.get_bond_active_slaves(bond_port)
        for slave in slaves['active']:
            slave_map_hash = self.slave_map_hash(slave, order_ports)
            self.verify(pkt_now[slave][0] == pkt_count * hash_values.count(slave_map_hash),
                        "XOR load balance transmit error on the link up port")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0,
                        "XOR load balance transmit error on the link down port")

    def test_xor_tx(self):
        """
        Verify that transmitting packets correctly in the XOR mode.
        """
        bond_port = self.create_bonded_device(MODE_XOR_BALANCE, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")

        slaves = {}
        slaves['active'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
        slaves['inactive'] = []

        self.verify_xor_tx(self.dut_ports[3], bond_port, "L2", False, **slaves)

    def test_xor_tx_one_slave_down(self):
        """
        Verify that transmitting packets correctly in the XOR mode,
        when bringing any one slave of the bonding device link down.
        """
        bond_port = self.create_bonded_device(MODE_XOR_BALANCE, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[2], self.dut_ports[1])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")

        try:
            slaves = {}
            slaves['active'] = [self.dut_ports[1], self.dut_ports[2]]
            slaves['inactive'] = [self.dut_ports[0]]

            self.verify_xor_tx(self.dut_ports[3], bond_port, "L2", False, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")

    def test_xor_tx_all_slaves_down(self):
        """
        Verify that transmitting packets correctly in the XOR mode,
        when bringing all slaves of the bonding device link down.
        """
        bond_port = self.create_bonded_device(MODE_XOR_BALANCE, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "down")

        try:
            slaves = {}
            slaves['active'] = []
            slaves['inactive'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]

            self.verify_xor_tx(self.dut_ports[3], bond_port, "L2", False, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "up")

    def vlan_strip_and_filter(self, action='off', *ports):
        """
        Open or shutdown the vlan strip and filter option of specified port.
        """
        for port_id in ports:
            self.dut.send_expect("vlan set strip %s %d" % (action, port_id), "testpmd> ")
            self.dut.send_expect("vlan set filter %s %d" % (action, port_id), "testpmd> ")

    def test_xor_l34_forward(self):
        """
        Verify that transmitting packets correctly in the XOR mode,
        when choosing the l34 as the load balance policy.
        """
        bond_port = self.create_bonded_device(MODE_XOR_BALANCE, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.set_balance_policy_for_bonding_device(bond_port, "l34")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")

        slaves = {}
        slaves['active'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
        slaves['inactive'] = []

        self.verify_xor_tx(self.dut_ports[3], bond_port, "L34", False, **slaves)
        self.vlan_strip_and_filter('off', self.dut_ports[0], self.dut_ports[1], self.dut_ports[2], self.dut_ports[3], bond_port)
        self.verify_xor_tx(self.dut_ports[3], bond_port, "L34", True, **slaves)

    def test_xor_rx(self):
        """
        Verify that receiving packets correctly in the XOR mode.
        """
        bond_port = self.create_bonded_device(MODE_XOR_BALANCE, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")

        slaves = {}
        slaves['active'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
        slaves['inactive'] = []

        self.verify_xor_rx(self.dut_ports[3], bond_port, **slaves)

    def verify_broadcast_rx(self, unbound_port, bond_port, **slaves):
        """
        Verify that receiving packets correctly in the broadcast mode.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** slaves:
        ******* 'active':[]
        ******* 'inactive':[]
        """
        pkt_count = 100
        pkt_now = {}

        pkt_now, summary = self.send_default_packet_to_slave(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        for slave in slaves['active']:
            self.verify(pkt_now[slave][0] == pkt_count, "Slave RX packet not correct in mode 3")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0, "Slave RX packet not correct in mode 3")
        self.verify(pkt_now[unbound_port][0] == pkt_count * len(slaves['active']),
                    "Unbonded port TX packet not correct in mode 3")
        self.verify(pkt_now[bond_port][0] == pkt_count * len(slaves['active']),
                    "Bonded device RX packet not correct in mode 3")

    def verify_broadcast_tx(self, unbound_port, bond_port, **slaves):
        """
        Verify that transmitting packets correctly in the broadcast mode.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** slaves:
        ******* 'actvie':[]
        ******* 'inactive':[]
        """
        pkt_count = 100
        pkt_now = {}

        pkt_now, summary = self.send_default_packet_to_unbound_port(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        for slave in slaves['active']:
            self.verify(pkt_now[slave][0] == pkt_count, "Slave TX packet not correct in mode 3")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0, "Slave TX packet not correct in mode 3")
        self.verify(pkt_now[unbound_port][0] == pkt_count, "Unbonded port RX packet not correct in mode 3")
        self.verify(pkt_now[bond_port][0] == pkt_count * len(slaves['active']),
                    "Bonded device TX packet not correct in mode 3")

    def test_broadcast_rx_tx(self):
        """
        Verify receiving and transmitting packets correctly in the broadcast mode.
        """
        bond_port = self.create_bonded_device(MODE_BROADCAST, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")

        slaves = {}
        slaves['active'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
        slaves['inactive'] = []

        self.verify_broadcast_rx(self.dut_ports[3], bond_port, **slaves)
        self.verify_broadcast_tx(self.dut_ports[3], bond_port, **slaves)

    def test_broadcast_tx_one_slave_down(self):
        """
        Verify that transmitting packets correctly in the broadcast mode,
        when bringing any one slave of the bonding device link down.
        """
        bond_port = self.create_bonded_device(MODE_BROADCAST, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")

        try:
            slaves = {}
            slaves['active'] = [self.dut_ports[1], self.dut_ports[2]]
            slaves['inactive'] = [self.dut_ports[0]]

            self.verify_broadcast_tx(self.dut_ports[3], bond_port, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")

    def test_broadcast_tx_all_slaves_down(self):
        """
        Verify that transmitting packets correctly in the broadcast mode,
        when bringing all slaves of the bonding device link down.
        """
        bond_port = self.create_bonded_device(MODE_BROADCAST, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False, self.dut_ports[0], self.dut_ports[1], self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "down")

        try:
            slaves = {}
            slaves['active'] = []
            slaves['inactive'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]

            self.verify_broadcast_tx(self.dut_ports[3], bond_port, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "up")

    def verify_lacp_rx(self, unbound_port, bond_port, **slaves):
        """
        Verify that receiving packets correctly in the mode 4.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** slaves:
        ******* 'active':[]
        ******* 'inactive':[]
        """
        pkt_count = 100
        pkt_now = {}
        summary = 0

        if len(slaves['active']):
            active_flag = 1
        else:
            active_flag = 0

        pkt_now, summary = self.send_customized_packet_to_bond_port(unbound_port, bond_port, self.tester_bond, pkt_count, **slaves)

        active_summary = 0
        for slave in slaves['active']:
            active_summary += pkt_now[slave][0]
        self.verify(active_summary >= summary * active_flag,
                    "Active slave have incorrect RX packet number in LACP")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0,
                        "Inactive slave have incorrect RX packet number in LACP")
        self.dut.send_expect("show port info %d" % self.dut_ports[3], "testpmd> ")
        self.verify(pkt_now[unbound_port][0] == summary * active_flag,
                    "Unbonded device has incorrect TX packet number in LACP")

    def verify_lacp_tx(self, unbound_port, bond_port, policy, vlan_tag=False, **slaves):
        """
        Verify that transmitting the packets correctly in the XOR mode.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** policy:'L2' , 'L23' or 'L34'
        *** vlan_tag:False or True
        *** slaves:
        ******* 'active'=[]
        ******* 'inactive'=[]
        """
        pkt_count = 100
        pkt_now = {}

        pkt_now, summary = self.send_customized_packet_to_unbound_port(unbound_port, bond_port, policy, vlan_tag=False, pkt_count=pkt_count, **slaves)

        hash_values = []
        hash_values = self.policy_and_slave_hash(policy, **slaves)

        order_ports = self.get_bond_active_slaves(bond_port)
        for slave in slaves['active']:
            slave_map_hash = self.slave_map_hash(slave, order_ports)
            self.verify(pkt_now[slave][0] >= pkt_count * hash_values.count(slave_map_hash),
                        "LACP load balance transmit incorrectly on the link up port")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0,
                        "LACP load balance transmit incorrectly on the link down port")
        self.verify(pkt_now[unbound_port][0] == summary,
                    "LACP load balance receive incorrectly on the unbound port")

    def add_linux_bond_device(self, bond_mode, bond_name='bond0', *tester_local_ports):
        if self.tester.get_os_type() == "linux":
            self.tester.send_expect("modprobe bonding mode=%d miimon=100" %
                                    int(bond_mode), "# ")
            self.tester.send_expect("ifconfig %s up" % bond_name, "# ")

            tester_bond_intfs = [self.tester.get_interface(port) for port in tester_local_ports]
            for intf in tester_bond_intfs:
                self.tester.send_expect("ifenslave -f %s %s" %
                                        (bond_name, intf), "# ")
                if not self.slave_is_here_linux(bond_name, intf):
                    self.verify(False, "Add linux bond device failed")
            for port in tester_local_ports:
                self.admin_tester_port(port, "up")
        else:
            self.verify(False,
                        "Not support to verify LACP on OS %s" % self.tester.get_os_type())

    def detach_linux_bond_device(self, bond_name='bond0', *tester_local_ports):
        tester_bond_intf = [self.tester.get_interface(port) for port in tester_local_ports]
        if self.tester.get_os_type() == "linux":
            for intf in tester_bond_intf:
                if self.slave_is_here_linux(bond_name, intf):
                    self.tester.send_expect("ifenslave -d %s %s" % (bond_name, intf),
                                            "# ")
                if self.slave_is_here_linux(bond_name, intf):
                    self.verify(False, "Delete linux bond device failed")
            for port in tester_local_ports:
                self.admin_tester_port(port, "up")
        else:
            self.verify(False,
                        "Not support to verify LACP on OS %s" % self.tester.get_os_type())

    def slave_is_here_linux(self, bond_name="bond0", *interfaces):
        out = self.tester.send_expect("cat /proc/net/bonding/%s" % bond_name,
                                      "# ")
        for intf in interfaces:
            if re.search(intf, out):
                return True
            else:
                return False

    def setup_and_clear_lacp(func):
        """
        Setting lacp test environment on tester.
        """
        @wraps(func)
        def test_env(*args, **kwargs):
            pmd_bond_instance = args[0]
            try:
                dut_ports = [pmd_bond_instance.dut_ports[port] for port in [0, 1, 2]]
                tester = pmd_bond_instance.tester
                tester_local_ports = [tester.get_local_port(port) for port in dut_ports]

                pmd_bond_instance.add_linux_bond_device(MODE_LACP,
                                                        pmd_bond_instance.tester_bond,
                                                        *tester_local_ports)

                func(*args, **kwargs)
            finally:
                pmd_bond_instance.detach_linux_bond_device(pmd_bond_instance.tester_bond,
                                                           *tester_local_ports)

        return test_env

    def just_clear_lacp(func):
        """

        """
        @wraps(func)
        def clear_env(*args, **kwargs):
            pmd_bond_instance = args[0]
            try:
                dut_ports = [pmd_bond_instance.dut_ports[port] for port in [0, 1, 2]]
                tester = pmd_bond_instance.tester
                tester_local_ports = [tester.get_local_port(port) for port in dut_ports]

                func(*args, **kwargs)
            finally:
                pmd_bond_instance.detach_linux_bond_device(pmd_bond_instance.tester_bond,
                                                           *tester_local_ports)
        return clear_env

    def verify_tlb_rx(self, unbound_port, bond_port, **slaves):
        """
        Verify that receiving packets correctly in the mode 4.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** slaves:
        ******* 'active':[]
        ******* 'inactive':[]
        """
        pkt_count = 100
        pkt_now = {}

        slave_num = slaves['active'].__len__()
        if slave_num != 0:
            active_flag = 1
        else:
            active_flag = 0

        pkt_now, summary = self.send_default_packet_to_slave(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        self.verify(pkt_now[unbound_port][0] == pkt_count * active_flag, "Unbonded device has error TX packet in TLB")
        self.verify(pkt_now[bond_port][0] == pkt_count * slave_num, "Bounded device has error RX packet in TLB")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0, "Inactive slave has error RX packet in TLB")
        for slave in slaves['active']:
            self.verify(pkt_now[slave][0] == pkt_count, "Active slave has error RX packet in TLB")

    def verify_tlb_tx(self, unbound_port, bond_port, **slaves):
        """
        Verify that transmitting packets correctly in the broadcast mode.
        Parameters:
        *** unbound_port: the unbonded port id
        *** bond_port: the bonded device port id
        *** slaves:
        ******* 'actvie':[]
        ******* 'inactive':[]
        """
        pkt_count = 'MANY'
        pkt_now = {}

        # send to unbonded device
        pkt_now, summary = self.send_default_packet_to_unbound_port(unbound_port, bond_port, pkt_count=pkt_count, **slaves)

        active_slaves = len(slaves['active'])
        if active_slaves:
            mean = float(summary) / float(active_slaves)
            active_flag = 1
        else:
            active_flag = 0

        for slave in slaves['active']:
            self.verify(pkt_now[slave][0] > mean * 0.9 and
                        pkt_now[slave][0] < mean * 1.1,
                        "Slave TX packet not correct in mode 5")
        for slave in slaves['inactive']:
            self.verify(pkt_now[slave][0] == 0, "Slave TX packet not correct in mode 5")
        self.verify(pkt_now[unbound_port][0] == summary,
                    "Unbonded port RX packet not correct in TLB")
        self.verify(pkt_now[bond_port][0] == summary * active_flag,
                    "Bonded device TX packet not correct in TLB")

    def test_tlb_basic(self):
        self.verify_bound_basic_opt(MODE_TLB_BALANCE)
        self.verify_bound_mac_opt(MODE_TLB_BALANCE)
        self.verify_bound_promisc_opt(MODE_TLB_BALANCE)

    def test_tlb_rx_tx(self):
        bond_port = self.create_bonded_device(MODE_TLB_BALANCE, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False,
                                         self.dut_ports[0],
                                         self.dut_ports[1],
                                         self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")

        slaves = {}
        slaves['active'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]
        slaves['inactive'] = []

        self.verify_tlb_rx(self.dut_ports[3], bond_port, **slaves)
        self.verify_tlb_tx(self.dut_ports[3], bond_port, **slaves)

    def test_tlb_one_slave_dwon(self):
        bond_port = self.create_bonded_device(MODE_TLB_BALANCE, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False,
                                         self.dut_ports[0],
                                         self.dut_ports[1],
                                         self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")

        try:
            slaves = {}
            slaves['active'] = [self.dut_ports[1], self.dut_ports[2]]
            slaves['inactive'] = [self.dut_ports[0]]

            self.verify_tlb_rx(self.dut_ports[3], bond_port, **slaves)
            self.verify_tlb_tx(self.dut_ports[3], bond_port, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")

    def test_tlb_all_slaves_down(self):
        bond_port = self.create_bonded_device(MODE_TLB_BALANCE, SOCKET_0)
        self.add_slave_to_bonding_device(bond_port, False,
                                         self.dut_ports[0],
                                         self.dut_ports[1],
                                         self.dut_ports[2])
        self.dut.send_expect("set portlist %d,%d" % (self.dut_ports[3], bond_port), "testpmd> ")
        self.start_all_ports()
        self.dut.send_expect("start", "testpmd> ")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "down")
        self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "down")

        try:
            slaves = {}
            slaves['active'] = []
            slaves['inactive'] = [self.dut_ports[0], self.dut_ports[1], self.dut_ports[2]]

            self.verify_tlb_rx(self.dut_ports[3], bond_port, **slaves)
            self.verify_tlb_tx(self.dut_ports[3], bond_port, **slaves)
        finally:
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[0]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[1]), "up")
            self.admin_tester_port(self.tester.get_local_port(self.dut_ports[2]), "up")

    def tear_down(self):
        """
        Run after each test case.
        """
        self.dut.send_expect("quit", "# ")

    def tear_down_all(self):
        """
        Run after each test suite.
        """
        pass