pkt_gen: STC - Imix Genome Support
[vswitchperf.git] / core / traffic_controller_rfc2544.py
1 # Copyright 2015-2017 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """RFC2544 Traffic Controller implementation.
15 """
16 from core.traffic_controller import TrafficController
17 from core.results.results import IResults
18 from conf import settings
19
20
21 class TrafficControllerRFC2544(TrafficController, IResults):
22     """Traffic controller for RFC2544 traffic
23
24     Used to setup and control a traffic generator for an RFC2544 deployment
25     traffic scenario.
26     """
27
28     def __init__(self, traffic_gen_class):
29         """Initialise the trafficgen and store.
30
31         :param traffic_gen_class: The traffic generator class to be used.
32         """
33         super().__init__(traffic_gen_class)
34         self._type = 'rfc2544'
35         self._tests = None
36
37     def configure(self, traffic):
38         """See TrafficController for description
39         """
40         super().configure(traffic)
41         self._tests = int(settings.getValue('TRAFFICGEN_RFC2544_TESTS'))
42
43     def send_traffic(self, traffic):
44         """See TrafficController for description
45         """
46         if not self.traffic_required():
47             return
48
49         super().send_traffic(traffic)
50
51         for packet_size in self._packet_sizes:
52             # Merge framesize with the default traffic definition
53             if 'l2' in traffic:
54                 traffic['l2'] = dict(traffic['l2'],
55                                      **{'framesize': packet_size})
56             else:
57                 traffic['l2'] = {'framesize': packet_size}
58
59             if traffic['traffic_type'] == 'rfc2544_back2back':
60                 result = self._traffic_gen_class.send_rfc2544_back2back(
61                     traffic, tests=self._tests, duration=self._duration, lossrate=self._lossrate)
62             elif traffic['traffic_type'] == 'rfc2544_continuous':
63                 result = self._traffic_gen_class.send_cont_traffic(
64                     traffic, duration=self._duration)
65             elif traffic['traffic_type'] == 'burst':
66                 result = self._traffic_gen_class.send_burst_traffic(
67                     traffic, duration=self._duration)
68             elif traffic['traffic_type'] == 'rfc2544_throughput':
69                 result = self._traffic_gen_class.send_rfc2544_throughput(
70                     traffic, tests=self._tests, duration=self._duration, lossrate=self._lossrate)
71             else:
72                 raise RuntimeError("Unsupported traffic type {} was "
73                                    "detected".format(traffic['traffic_type']))
74
75             result = self._append_results(result, packet_size)
76             self._results.append(result)
77
78     def send_traffic_async(self, traffic, function):
79         """See TrafficController for description
80         """
81         if not self.traffic_required():
82             return
83
84         super().send_traffic_async(traffic, function)
85
86         for packet_size in self._packet_sizes:
87             traffic['l2'] = {'framesize': packet_size}
88             self._traffic_gen_class.start_rfc2544_throughput(
89                 traffic,
90                 tests=self._tests,
91                 duration=self._duration)
92             self._traffic_started = True
93             if function['args']:
94                 function['function'](function['args'])
95             else:
96                 function['function']()
97             result = self._traffic_gen_class.wait_rfc2544_throughput()
98             result = self._append_results(result, packet_size)
99             self._results.append(result)