stl_rx_test.py revision 03d70c42
1#!/router/bin/python
2from .stl_general_test import CStlGeneral_Test, CTRexScenario
3from trex_stl_lib.api import *
4import os, sys
5
6class STLRX_Test(CStlGeneral_Test):
7    """Tests for RX feature"""
8
9    def setUp(self):
10        per_driver_params = {"rte_vmxnet3_pmd": [1, 50], "rte_ixgbe_pmd": [30, 5000], "rte_i40e_pmd": [80, 5000],
11                             "rte_igb_pmd": [80, 500], "rte_em_pmd": [1, 50], "rte_virtio_pmd": [1, 50]}
12
13        CStlGeneral_Test.setUp(self)
14        assert 'bi' in CTRexScenario.stl_ports_map
15
16        self.c = CTRexScenario.stl_trex
17
18        self.tx_port, self.rx_port = CTRexScenario.stl_ports_map['bi'][0]
19
20        port_info = self.c.get_port_info(ports = self.rx_port)[0]
21        cap = port_info['rx']['caps']
22        print cap
23        if "flow_stats" not in cap or "latency" not in cap:
24            self.skip('port {0} does not support RX'.format(self.rx_port))
25        self.cap = cap
26
27        self.rate_percent = per_driver_params[port_info['driver']][0]
28        self.total_pkts = per_driver_params[port_info['driver']][1]
29        self.c.reset(ports = [self.tx_port, self.rx_port])
30
31        self.pkt = STLPktBuilder(pkt = Ether()/IP(src="16.0.0.1",dst="48.0.0.1")/UDP(dport=12,sport=1025)/IP()/'a_payload_example')
32
33    @classmethod
34    def tearDownClass(cls):
35        # connect back at end of tests
36        if not cls.is_connected():
37            CTRexScenario.stl_trex.connect()
38
39
40    def __verify_flow (self, pg_id, total_pkts, pkt_len, stats):
41        flow_stats = stats['flow_stats'].get(pg_id)
42        latency_stats = stats['latency'].get(pg_id)
43
44        if not flow_stats:
45            assert False, "no flow stats available"
46
47        tx_pkts  = flow_stats['tx_pkts'].get(self.tx_port, 0)
48        tx_bytes = flow_stats['tx_bytes'].get(self.tx_port, 0)
49        rx_pkts  = flow_stats['rx_pkts'].get(self.rx_port, 0)
50        if latency_stats is not None:
51            drops = latency_stats['err_cntrs']['dropped']
52            ooo = latency_stats['err_cntrs']['out_of_order']
53            if drops != 0 or ooo != 0:
54                pprint.pprint(latency_stats)
55                tmp='Dropped or out of order packets - dropped: {0}, ooo: {1}'.format(drops, ooo)
56                assert False, tmp
57
58        if tx_pkts != total_pkts:
59            pprint.pprint(flow_stats)
60            tmp = 'TX pkts mismatch - got: {0}, expected: {1}'.format(tx_pkts, total_pkts)
61            assert False, tmp
62
63        if tx_bytes != (total_pkts * pkt_len):
64            pprint.pprint(flow_stats)
65            tmp = 'TX bytes mismatch - got: {0}, expected: {1}'.format(tx_bytes, (total_pkts * pkt_len))
66            assert False, tmp
67
68        if rx_pkts != total_pkts:
69            pprint.pprint(flow_stats)
70            tmp = 'RX pkts mismatch - got: {0}, expected: {1}'.format(rx_pkts, total_pkts)
71            assert False, tmp
72
73        if "rx_bytes" in self.cap:
74            rx_bytes = flow_stats['rx_bytes'].get(self.rx_port, 0)
75            if rx_bytes != (total_pkts * pkt_len):
76                pprint.pprint(flow_stats)
77                tmp = 'RX bytes mismatch - got: {0}, expected: {1}'.format(rx_bytes, (total_pkts * pkt_len))
78                assert False, tmp
79
80
81    # RX itreation
82    def __rx_iteration (self, exp_list):
83
84        self.c.clear_stats()
85
86        self.c.start(ports = [self.tx_port])
87        self.c.wait_on_traffic(ports = [self.tx_port])
88        stats = self.c.get_stats()
89
90        for exp in exp_list:
91            self.__verify_flow(exp['pg_id'], exp['total_pkts'], exp['pkt_len'], stats)
92
93
94    # one simple stream on TX --> RX
95    def test_one_stream(self):
96        total_pkts = self.total_pkts * 10
97
98        try:
99            s1 = STLStream(name = 'rx',
100                           packet = self.pkt,
101                           flow_stats = STLFlowLatencyStats(pg_id = 5),
102                           mode = STLTXSingleBurst(total_pkts = total_pkts,
103                                                   percentage = self.rate_percent
104                                                   ))
105
106            # add both streams to ports
107            self.c.add_streams([s1], ports = [self.tx_port])
108
109            print("\ninjecting {0} packets on port {1}\n".format(total_pkts, self.tx_port))
110
111            exp = {'pg_id': 5, 'total_pkts': total_pkts, 'pkt_len': self.pkt.get_pkt_len()}
112
113            self.__rx_iteration( [exp] )
114
115
116        except STLError as e:
117            assert False , '{0}'.format(e)
118
119
120    # one simple stream on TX --> RX
121    def test_multiple_streams(self):
122        num_latency_streams = 128
123        num_flow_stat_streams = 127
124        total_pkts = int(self.total_pkts / num_latency_streams)
125        if total_pkts == 0:
126            total_pkts = 1
127        percent = float(self.rate_percent) / num_latency_streams
128
129        try:
130            streams = []
131            exp = []
132            # 10 identical streams
133            for pg_id in range(1, num_latency_streams):
134
135                streams.append(STLStream(name = 'rx {0}'.format(pg_id),
136                                         packet = self.pkt,
137                                         flow_stats = STLFlowLatencyStats(pg_id = pg_id),
138                                         mode = STLTXSingleBurst(total_pkts = total_pkts+pg_id, percentage = percent)))
139
140                exp.append({'pg_id': pg_id, 'total_pkts': total_pkts+pg_id, 'pkt_len': self.pkt.get_pkt_len()})
141
142            for pg_id in range(num_latency_streams + 1, num_latency_streams + num_flow_stat_streams):
143
144                streams.append(STLStream(name = 'rx {0}'.format(pg_id),
145                                         packet = self.pkt,
146                                         flow_stats = STLFlowStats(pg_id = pg_id),
147                                         mode = STLTXSingleBurst(total_pkts = total_pkts+pg_id, percentage = percent)))
148
149                exp.append({'pg_id': pg_id, 'total_pkts': total_pkts+pg_id, 'pkt_len': self.pkt.get_pkt_len()})
150
151            # add both streams to ports
152            self.c.add_streams(streams, ports = [self.tx_port])
153
154            self.__rx_iteration(exp)
155
156
157        except STLError as e:
158            assert False , '{0}'.format(e)
159
160    def test_1_stream_many_iterations (self):
161        total_pkts = self.total_pkts
162
163        try:
164            s1 = STLStream(name = 'rx',
165                           packet = self.pkt,
166                           flow_stats = STLFlowLatencyStats(pg_id = 5),
167                           mode = STLTXSingleBurst(total_pkts = total_pkts,
168                                                   percentage = self.rate_percent
169                                                   ))
170
171            # add both streams to ports
172            self.c.add_streams([s1], ports = [self.tx_port])
173
174            print "\ninjecting {0} packets on port {1}\n".format(total_pkts, self.tx_port)
175
176            exp = {'pg_id': 5, 'total_pkts': total_pkts, 'pkt_len': self.pkt.get_pkt_len()}
177
178            for i in range(0, 10):
179                print("starting iteration {0}".format(i))
180                self.__rx_iteration( [exp] )
181
182
183
184        except STLError as e:
185            assert False , '{0}'.format(e)
186