trex_rx_test.py revision 04300e56
1#!/router/bin/python
2from .trex_general_test import CTRexGeneral_Test
3from CPlatform import CStaticRouteConfig, CNatConfig
4from .tests_exceptions import *
5#import sys
6import time
7import copy
8from nose.tools import nottest
9import traceback
10
11class CTRexRx_Test(CTRexGeneral_Test):
12    """This class defines the rx testcase of the TRex traffic generator"""
13    def __init__(self, *args, **kwargs):
14        CTRexGeneral_Test.__init__(self, *args, **kwargs)
15        self.unsupported_modes = ['virt_nics'] # TODO: fix
16
17    def setUp(self):
18        CTRexGeneral_Test.setUp(self)
19
20
21    def check_rx_errors(self, trex_res, allow_error_tolerance = True):
22        try:
23            # counters to check
24
25            latency_counters_display = {'m_unsup_prot': 0, 'm_no_magic': 0, 'm_no_id': 0, 'm_seq_error': 0, 'm_length_error': 0, 'm_no_ipv4_option': 0, 'm_tx_pkt_err': 0}
26            rx_counters = {'m_err_drop': 0, 'm_err_aged': 0, 'm_err_no_magic': 0, 'm_err_wrong_pkt_id': 0, 'm_err_fif_seen_twice': 0, 'm_err_open_with_no_fif_pkt': 0, 'm_err_oo_dup': 0, 'm_err_oo_early': 0, 'm_err_oo_late': 0, 'm_err_flow_length_changed': 0}
27
28            # get relevant TRex results
29
30            try:
31                ports_names = trex_res.get_last_value('trex-latecny-v2.data', 'port\-\d+')
32                if not ports_names:
33                    raise AbnormalResultError('Could not find ports info in TRex results, path: trex-latecny-v2.data.port-*')
34                for port_name in ports_names:
35                    path = 'trex-latecny-v2.data.%s.stats' % port_name
36                    port_result = trex_res.get_last_value(path)
37                    if not port_result:
38                        raise AbnormalResultError('Could not find port stats in TRex results, path: %s' % path)
39                    for key in latency_counters_display:
40                        latency_counters_display[key] += port_result[key]
41
42                # using -k flag in TRex produces 1 error per port in latency counter m_seq_error, allow it until issue resolved. For comparing use dict with reduces m_seq_error number.
43                latency_counters_compare = copy.deepcopy(latency_counters_display)
44                latency_counters_compare['m_seq_error'] = max(0, latency_counters_compare['m_seq_error'] - len(ports_names))
45
46                path = 'rx-check.data.stats'
47                rx_check_results = trex_res.get_last_value(path)
48                if not rx_check_results:
49                    raise AbnormalResultError('No TRex results by path: %s' % path)
50                for key in rx_counters:
51                    rx_counters[key] = rx_check_results[key]
52
53                path = 'rx-check.data.stats.m_total_rx'
54                total_rx = trex_res.get_last_value(path)
55                if total_rx is None:
56                    raise AbnormalResultError('No TRex results by path: %s' % path)
57                elif not total_rx:
58                    raise AbnormalResultError('Total rx_check (%s) packets is zero.' % path)
59
60                print('Total packets checked: %s' % total_rx)
61                print('Latency counters: %s' % latency_counters_display)
62                print('rx_check counters: %s' % rx_counters)
63
64            except KeyError as e:
65                self.fail('Expected key in TRex result was not found.\n%s' % traceback.print_exc())
66
67            # the check. in loopback expect 0 problems, at others allow errors <error_tolerance>% of total_rx
68
69            total_errors = sum(rx_counters.values()) + sum(latency_counters_compare.values())
70            error_tolerance = self.get_benchmark_param('error_tolerance')
71            if not error_tolerance:
72                if not allow_error_tolerance:
73                    error_tolerance = 0
74                else:
75                    error_tolerance = 0.1
76            error_percentage = total_errors * 100.0 / total_rx
77
78            if total_errors > 0:
79                if error_percentage > error_tolerance:
80                    self.fail('Too much errors in rx_check. (~%s%% of traffic)' % error_percentage)
81                else:
82                    print('There are errors in rx_check (%f%%), not exceeding allowed limit (%s%%)' % (error_percentage, error_tolerance))
83            else:
84                print('No errors in rx_check.')
85        except Exception as e:
86            print(traceback.print_exc())
87            self.fail('Errors in rx_check: %s' % e)
88
89    def test_rx_check_sfr(self):
90        if not self.is_loopback:
91            self.router.configure_basic_interfaces()
92            self.router.config_pbr(mode = 'config')
93
94        core  = self.get_benchmark_param('cores')
95        mult  = self.get_benchmark_param('multiplier')
96        sample_rate = self.get_benchmark_param('rx_sample_rate')
97
98        ret = self.trex.start_trex(
99            c = core,
100            m = mult,
101            p = True,
102            nc = True,
103            rx_check = sample_rate,
104            d = 100,
105            f = 'avl/sfr_delay_10_1g_no_bundeling.yaml',
106            l = 1000,
107            k = 10,
108            learn_verify = True,
109            l_pkt_mode = 2)
110
111        trex_res = self.trex.sample_to_run_finish()
112
113        print("\nLATEST RESULT OBJECT:")
114        print(trex_res)
115        #print ("\nLATEST DUMP:")
116        #print trex_res.get_latest_dump()
117
118        self.check_general_scenario_results(trex_res)
119        self.check_CPU_benchmark(trex_res)
120        self.check_rx_errors(trex_res)
121
122
123    def test_rx_check_http(self):
124        if not self.is_loopback:
125            # TODO: skip as test_rx_check_http_negative will cover it
126            #self.skip('This test is covered by test_rx_check_http_negative')
127            self.router.configure_basic_interfaces()
128            self.router.config_pbr(mode = "config")
129
130        core  = self.get_benchmark_param('cores')
131        mult  = self.get_benchmark_param('multiplier')
132        sample_rate = self.get_benchmark_param('rx_sample_rate')
133
134        ret = self.trex.start_trex(
135            c = core,
136            m = mult,
137            p  = True,
138            nc = True,
139            rx_check = sample_rate,
140            d = 100,
141            f = 'cap2/http_simple.yaml',
142            l = 1000,
143            k = 10,
144            learn_verify = True,
145            l_pkt_mode = 2)
146
147        trex_res = self.trex.sample_to_run_finish()
148
149        print("\nLATEST RESULT OBJECT:")
150        print(trex_res)
151
152        self.check_general_scenario_results(trex_res)
153        self.check_CPU_benchmark(trex_res)
154        self.check_rx_errors(trex_res)
155
156
157    def test_rx_check_sfr_ipv6(self):
158        if not self.is_loopback:
159            self.router.configure_basic_interfaces()
160            self.router.config_pbr(mode = 'config')
161            self.router.config_ipv6_pbr(mode = "config")
162
163        core  = self.get_benchmark_param('cores')
164        mult  = self.get_benchmark_param('multiplier')
165        sample_rate = self.get_benchmark_param('rx_sample_rate')
166
167        ret = self.trex.start_trex(
168            c = core,
169            m = mult,
170            p = True,
171            nc = True,
172            rx_check = sample_rate,
173            d = 100,
174            f = 'avl/sfr_delay_10_1g_no_bundeling.yaml',
175            l = 1000,
176            k = 10,
177            ipv6 = True)
178
179        trex_res = self.trex.sample_to_run_finish()
180
181        print("\nLATEST RESULT OBJECT:")
182        print(trex_res)
183        #print ("\nLATEST DUMP:")
184        #print trex_res.get_latest_dump()
185
186        self.check_general_scenario_results(trex_res)
187        self.check_CPU_benchmark(trex_res)
188        self.check_rx_errors(trex_res)
189
190
191    def test_rx_check_http_ipv6(self):
192        if not self.is_loopback:
193            self.router.configure_basic_interfaces()
194            self.router.config_pbr(mode = "config")
195            self.router.config_ipv6_pbr(mode = "config")
196
197        core  = self.get_benchmark_param('cores')
198        mult  = self.get_benchmark_param('multiplier')
199        sample_rate = self.get_benchmark_param('rx_sample_rate')
200
201        ret = self.trex.start_trex(
202            c = core,
203            m = mult,
204            p  = True,
205            nc = True,
206            rx_check = sample_rate,
207            d = 100,
208            f = 'cap2/http_simple.yaml',
209            l = 1000,
210            k = 10,
211            ipv6 = True)
212
213        trex_res = self.trex.sample_to_run_finish()
214
215        print("\nLATEST RESULT OBJECT:")
216        print(trex_res)
217
218        self.check_general_scenario_results(trex_res)
219        self.check_CPU_benchmark(trex_res)
220        self.check_rx_errors(trex_res)
221
222    #@nottest
223    def test_rx_check_http_negative(self):
224        if self.is_loopback:
225            self.skip('This test uses NAT, not relevant for loopback')
226
227        self.router.configure_basic_interfaces()
228        self.router.config_pbr(mode = "config")
229
230        core  = self.get_benchmark_param('cores')
231        mult  = self.get_benchmark_param('multiplier')
232        sample_rate = self.get_benchmark_param('rx_sample_rate')
233
234        ret = self.trex.start_trex(
235            c = core,
236            m = mult,
237            p = True,
238            rx_check = sample_rate,
239            d = 60,
240            f = 'cap2/http_simple.yaml',
241            l = 1000,
242            k = 10,
243            learn_verify = True,
244            l_pkt_mode = 2)
245
246        print('Run for 40 seconds, expect no errors')
247        trex_res = self.trex.sample_x_seconds(40)
248        print("\nLATEST RESULT OBJECT:")
249        print(trex_res)
250        self.check_general_scenario_results(trex_res)
251        self.check_CPU_benchmark(trex_res)
252        self.check_rx_errors(trex_res)
253
254        print('Run until finish, expect errors')
255        old_errors = copy.deepcopy(self.fail_reasons)
256        nat_dict = self.get_benchmark_param('nat_dict', test_name = 'test_nat_simple_mode1')
257        nat_obj  = CNatConfig(nat_dict)
258        self.router.config_nat(nat_obj)
259        self.router.config_zbf()
260        trex_res = self.trex.sample_to_run_finish()
261        self.router.config_no_zbf()
262        self.router.config_no_nat(nat_obj)
263        #self.router.clear_nat_translations()
264        print("\nLATEST RESULT OBJECT:")
265        print(trex_res)
266        self.check_rx_errors(trex_res, allow_error_tolerance = False)
267        if self.fail_reasons == old_errors:
268            self.fail('Expected errors here, got none.')
269        else:
270            print('Got errors as expected.')
271            self.fail_reasons = old_errors
272
273    def tearDown(self):
274        CTRexGeneral_Test.tearDown(self)
275        pass
276
277if __name__ == "__main__":
278    pass
279